Posts

Difference between JDK, JRE and JVM

In this blog post, I am going to explain the JVM, JRE and JDK and how they differ from each other. JVM JVM stands for Java Virtual Machine . It provides the environment to execute the Java byte code. As you probably know, Java is platform independent i.e. the same Java class file can be run on any operating system. It is the JVM which makes Java platform independent. When you compile code written in other programming languages, it creates files that are operating system specific. When you compile Java code, it is compiled into platform independent bytecode which is present in the .class file created after compilation. The JVM understands and executes this bytecode. So the JVM its on top of the operating system and runs the bytecode making Java platform independent. JRE JRE stands for Java Runtime Environment . JVM executes the bytecode from the .class file. However, in addition to the JVM, some additional libraries or jar files are required. So the JRE consists of these libraries and ...

Singleton class Explained

Singleton is a design pattern. A singleton class is a class for which only one object can be created. You can create a singleton class by using a private constructor. The following code snippet demonstrates this: private static Singleton singletonInstance;private Singleton(){}public static Singleton getInstance(){if (singletonInstance == null){singletonInstance = new Singleton();}return singletonInstance;} Here, we have a private constructor and a public getInstance method. The getInstance checks if an instance already exists. If an instance does not exist, it creates one using the private constructor. If an instance exists, it just returns it. So this method ensures that there is only one instance of the Singleton class. Any external class that needs an instance of the Singleton class, should invoke the getInstance method.

Difference between Break and Continue statement

Both the break and continue statements are used to change the fliw of executionin Java loops. However they differ in the way they work. What is a break statement? A break statement is used to terminate a loop. So the moment a break statement is encountered, control is transferred outside the loop even if condition part of the loop is true. Consider the following code snippet: for(int i = 0; i < 50;i++){if(i == 10)break;System.out.println("i="+i);}System.out.println("Outside loop"); This will print the following output: i=0i=1i=2i=3i=4i=5i=6i=7i=8i=9Outside loop So the loop executes till i reaches 10. Once the value of i is 10, the loop is exited. In addition a break statement is also used in a switch block to skip further case statements the moment a match is encountered. What is a continue statement? Continue statement is used when you want to stop processing the remaining code in the body of a loop for a particular iteration but continue the loop for the next i...

Method Overloading Vs Method overridding

  In this blog post, I will be explaining the difference between method overloading and method overidding. What is method overloading? In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. The following code demonstrates this: public class AddDemo {public int add(int a,int b){return a+b;}public double add(double a,double b){return a+b;}} So here this class has two methods with the name “ add “. The first version accepts two integer values and returns an int, whereas the second version accepts two double values and returns a double. So both these methods have the same name i.e. add , but accept different types of parameters. Overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types...

How to read input from the user in Java

This blog post demonstrates how to read a user’s input in Java.  You can use the following code snippet: public class ReadInput {public static void main(String args[]){Scanner scanner = new Scanner(System.in);System.out.println("Enter your input:");int num = scanner.nextInt();System.out.println("The number entered is "+num);}}   This code uses the Scanner class. This class has a method called nextInt which is used to read an integer input. In addition to this method, it has several other methods like nextDouble , nextFloat  ,etc which can be used to read other data types.  In addition, it also has a method called next , which can be used to read String data.

System.out.println explained

In this blog post, I will be explaining the System.out.println statement which is the first statement you will come across when you start learning Java programming.   System System is a final class in the java.lang package. It has several useful classes and methods. In addition to supporting standard input and output, it has methods to access external properties, method for initiating garbage collection, etc.  It supports input/output by providing static variables corresponding to the input/output streams.   Out Out is a static variable in the System class. It is of the type PrintStream .  Since it is a static variable, it is instantiated at start up. It is mapped to the standard output which is the console. This stream is already open and ready to accept output data.   Println Println is a method within the PrintStream  class.  There are several overloaded versions of this method that accepts different types of data like String, integer, double, etc.  Each method prin...

Static keyword explained

In this blog post, I will be explaining Java’s static keyword. What is the static keyword? There will be times when you will want to define a class member i.e. either method or field independent of any object of that class. To create such a member, we need to precede its declaration with the   static keyword .  When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object, i.e. it can be accessed directly with the class name. You can declare both methods and variables to be static.   What is a Static Variable? A variable preceded with the static keyword is a static variable.  It can be accessed irrespective of any member directly using the class name. Variables declared as static are essentially, global variables. When objects of a class are created, no copy of a static variable is made. Instead, all objects of the class share the same static variable.   Static method A method preceded with the stati...