Posts

How to read a file on the classpath in Java

Image
There are often programming situations when you need to read a file on the classpath via Java. In this article, I will show you how to write code for this. Reading a file on the classpath Let us first take a look at the code that you need to write to read a file on the classpath. public class ReadFileFromClassPathDemo { public static void main(String[] args) throws IOException { // read in the file from the resources directory ClassLoader classLoader = ReadFileFromClassPathDemo.class.getClassLoader(); URL url = classLoader.getResource("input.txt"); String fileName = url.getFile(); File file = new File(fileName); String str = new String(Files.readAllBytes(file.toPath())); System.out.println(str); }} Java has an in-built class called ClassLoader .  It is used to dynamically load classes. It can also be used to load resources from the classpath. The code above first obtains a ClassLoader . It then invokes the getResource method on the classLoader . This l...

How to use Flyway with Spring Boot with code samples

Image
At its core, Flyway is a version control system for databases. It allows applying incremental changes to a database. In this article, we will be learning how to use Flyway with a Spring Boot application. Flyway Overview As explained earlier, Flyway is a version control system for databases. To explain in very simple words, it allows specifying database DDL/DML in an SQL file (also known as a migration). It then applies these changes to the database. In case you need to apply further changes, you need to specify another migration with a higher version number with the incremental changes. Flyway then applies the new changes to the database. Let us now understand how to use Flyway with Spring Boot and a MySQL database. Project Creation and Setup  Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the Spring Boot, Flyway, and MySQL dependencies . So, the  pom.xml  file should be similar to the following: <proj...

Tomcat Maven Plugin with code sample

Image
The Tomcat Maven plugin helps to run a Java web application in an embedded Tomcat container. Thus, this allows developers to quickly develop and build a web application without having to install Tomcat. In this article, we will be learning how to use this plugin. Creating a Maven Project Let us first create a simple Maven web project. I will be using Eclipse IDE. In order to create a Maven web project in Eclipse, you need to follow the steps given below: Step 1 – Click on File > New > Other . Click on Maven Project . Click Next : Step 2 – The following screen is displayed. Click Next : Step 3 – In the Filter text box, type “maven-archetype-webapp” .  Select “ org.apache.maven.archetypes “. Click Next : Step 4 – Enter Group Id and Artifact Id . Click Finish : This creates a web project with the required directory structure. It also has a basic hello world page:   Adding the Tomcat plugin In order to support the Tomcat Maven plug...

Python While Loop with code samples

Much like a Java while loop, you can use a Python while loop to repeatedly execute a block of code as long as a condition is true. In this article, we will learn more about the Python while loop. Python While loop Syntax A while loop in Python has the following syntax: while <condition>: loop body The while keyword is followed by a condition. If the condition evaluates to true, the body of the loop is executed. Once the body of the loop is completed, the condition is checked again and this is repeated until the condition evaluates to false at which point the loop is exited. While loop Sample Code The following code demonstrates how you can use the while loop: num=10counter=1total=0while counter<=num: total += counter counter+=1print("sum of first 10 numbers is",total) This code adds the numbers from 1 to 10 and prints the sum. It uses a while loop to iterate from 1 to 10. The while loop checks the condition counter <= num and if so adds the current value of count...

Parallel Streams in Java with code samples

In my earlier article, I had covered the Stream API in depth. In this article, we will be taking a look at parallel streams. Simply speaking, a parallel stream is a stream that operates on its elements in parallel. How to obtain a parallel stream Just like the stream method, all the Collection interfaces have a parallelStream method. This returns a parallel stream on the underlying Collection. Consider the following code: List<String> food = Arrays.asList("Pizza","Sandwich","Burger","Rice");Stream<String> foodStr = food.parallelStream(); This code first creates a String List food with some values. It then invokes the parallelStream method on food. This returns a String Stream which is a parallel stream. In addition, the Stream interface also has a method parallel . If the stream on which this method is invoked is a sequential stream, it returns a parallel stream corresponding to it. If the stream on which it is invoked in a paral...

Java Inheritance Interview Questions With Examples

In this article, I will be listing some Java inheritance interview questions What are the benefits of inheritance in Java? Answer: Inheritance has the following benefits: It permits code reuse. So, if there is some common code that can be used across a set of subclasses it can be specified in the base class and reused by all the subclasses. It facilitates polymorphism via method overriding. Method overriding allows a subclass to specify a method with the same type signature as a method in the superclass.  To know more about method overriding refer to this blog post. It makes the code extendable. So, if there is already some code, developers can use the code by creating a subclass and they need not write it again What are the different types of inheritance? Answer: Java supports the following types of inheritance: Single Inheritance – In this case, a class has only one subclass Multi-level Inheritance – In this case, a subclass in turn has a subclass Hierarchical Inheritance...

IntBinaryOperator

In this blog post, I will be explaining how the Java 8 functional interface  IntBinaryOperator works. To know more about functional interfaces, you can refer to this  blog post. What is IntBinaryOperator IntBinaryOperator is an in-built functional interface in the java.util.Function package. It accepts two int arguments, operates on it and produces a result of type int . It is a specialization of the BinaryOperator interface. It has a applyAsInt method. It applies the logic in this method on the int arguments passed in and produces an int result. IntBinaryOperator Code Sample The following code demonstrates this   interface: IntBinaryOperator intBinaryOp = (num1,num2) -> num1+num2;int input1=14,input2=12;int result = intBinaryOp.applyAsInt(input1, input2);System.out.println("result:"+result); Line 1 declares a IntBinaryOperator instance intBinaryOp and implements it via a lambda expression that adds the input arguments and returns the result. Line 3 invokes the applyA...