Tuesday, 23 August 2016

JVM Architecture:

JVM - Java Virtual Machine is used to interpret the .class file which is in byte code format to machine understandable format.

Machine understandable format is 0's and 1's. JVM architecture is given in the below diagram and its very important to understand the architecture.




JVM resides on the RAM of the machine which takes care of execution of the statement memory creation, object creation and releasing memory. This makes developers life very easy.

Class Files:

Java file which you write after compilation generated .class file. The .class file is a byte code which will not be in readable format. The class file is given to the JVM for interpreting it to machine understandable format.

Class Loader Subsystem:

Class loader loads the class files and if there is any issues in that file it will be prevented from further execution. This is the main reason for Java being secure. If there are any virus defined files this will not be allowed for execution by which virus is prevented. 

Method Area:

Java class contains methods and variables. Memory for methods and variables are allocated in the method area. 

Heap: 

Heap holds the memory for the object of the class.

Stack:

While executing the java class it needs memory for storing the intermediate results and intermediate processing. Java Stack is used for storing the intermediate values.

PC register:

These are teh registers which contains memory address of the instructions of the methods.

Native method stack:

Native methods are executed in this location.

Execution Engine = Interpreter and JIT


JIT - Just In Time Compiler.

Interpreter interprets the byte code and converts it into machine understandable format. This will arise a question why JIT is required.

JIT is used for processing the bulk data. Below is the clear example for this,

int a =10,b=13;
System.out.println(a);
System.out.println(b);

for(i=1;i<=a;i++)
{
System.out.println(a);
}


In the above example 'a' will be printed after interpreting by the interpreter which takes 2 nanoseconds. Similarly second statement 'b' is printed in 2 nano seconds.

In same manner for loop which prints same value 'a' in a loop for 10 times. This will take 2 X 10 nano seconds = 20 nanoseconds. This type of task is given to the JIT compiler. JIT will take 2 nanoseconds to print 'a' first time and then creates memory for next 9 iterations and pushes the same value to the memory, this will take another 2 nanoseconds. Totally 4 nanoseconds will be taken which reduces much of the time.

JVM decides which statement should be given to JIT and interpreter.



Interview Question:

Is java platform dependent?

No, java is not platform dependent. It can run on any platform.
Java is platform independent because you can develop Java code in one platform and can execute or run it in any another platform.
JDK-JRE-JVM :

Understanding the relationship between JDK(Java Development Kit) , JRE (Java Runtime Environment) and JVM(Java Virtual Machine) is very important since this forms the base of Java.
Everybody will have a confusion in this three terms on what this really mean. In most of the interviews, interviewer may ask this question to test the basic knowledge about Java. This will help you to understand why Java is platform independent.



JDK - Java Development Kit
     When you download java JDK and install it in your machine you can see JRE will be within JDK. JDK is a development kit which has the javac. Latest version of JDK is 8.


JDK is used for development of the code, running the code and displaying the result.




JDK = JRE + JVM

JDK = Development + Execution + Display

If a machine has JDK installed then it has the environment to develop the code, run the code and display the result.


JRE - Java Runtime Enviroment

  JRE is used for execution of the code. JRE internally contains JVM which interprets the byte code and converts it into machine understandable format.

JRE = Execution + Display


JVM - Java Virtual Machine is an abstract machine. This machine is used for execution of the bytecode. This converts the byte code and make its understandable by the machine. JVM is system dependent. There are many flavors of JVM for different Platforms and Operation system.

JVM architecture is very important to understand. I will explain the JVM architecture in future posts.




Thursday, 18 August 2016

Java 8 : Lambda Expression

Lambda Expression is one of the best feature given by Java 8.
It is used for functional programming.

Syntax :

parameter - > expression body

Note :
1) Optional Parenthesis : for the parameter if its single parameter. For multiple parameters you need to specify the the parenthesis.
2) Optional type declaration : type of the parameter need not be specified. The value that is returned by the function will take care of the parameter type.
3) Optional curly braces
4) Optional return type if the body has single expression of return type.
Java 8 Features:

Java 8 is the latest java version available. This has many new features within it. I have listed all the Java 8 features below.

1) Lambda Expression (parameter -> expression)
2) Method reference (::)
3) Functional reference (->)
4) Default method in Interface
5) Nashorn  Javascript engine for Java
6) Advanced Data and Time API
7) Stream API
8) Optional Class
9) Base64.

I will elaborate each and every feature in the upcoming post. 

Thursday, 4 August 2016

INTERVIEW QUESTION:

Can we create object for Abstract Class?

No, we cannot create object for an abstract class. Abstract class is an incomplete class for which we cannot create object.


Wednesday, 3 August 2016

Interface:

Interface is the blueprint for the class. It is a perfect example for Abstraction. It contains method which are abstract. Since its abstract the method will have only declaration and no method body.
All the data members of the interface are final and static by default and all the methods are abstract by default.

Interface can be implemented in a class. The class should contain the implementation of all the abstract methods.

Example:

public interface Animal{
int i = 4; //final and static
public void legs();
public void eat();
}

public class Lion implements Animal{
public void legs(){
System.out.println("Lion has "+i+" legs");
}
public void eat(){
System.out.println("Lion is a carnivorous");
}
}


Marker Interface:
Marker interface is an interface which is empty without any abstract method declaration.

public interface marker{
}

Tuesday, 2 August 2016

Abstract method: 

     Abstract method is a method without method body. "abstract" is the key word used to declare a abstract method. Abstract method body should be implemented in the class that extends it. Abstract method will be present in the abstract class.

Example:

public abstract class Example{

public abstract void abstractmethod(); //This is a abstract method without method body.
}
     

Abstract Class:

    Abstract class is not a complete class, its incomplete. We cannot create object for the Abstract class since its incomplete. The Abstract class can be extended by a sub class where all the abstract methods need to be implemented. Abstract class contains concrete methods, abstract method and instance variables.

 We can create object for the  sub class  which extends the Abstract class. Since the sub class is a complete class that has the implementation of the abstract methods. Subclass object can be given to the Abstract class reference.

Example:

/*
*Abstract class
*/
public abstract class Example {
static int i=10;//instance variables
public abstract void method(); //abstract method
public void add(){
   System.out.println("Concrete method");
 }
}

/*
*Sub class that extends the abstract class
*/
public class SubClass extends Example {

public void method(){
   System.out.println("This is a method implementation of the abstract method");
}

public static void main(String args[]){

   Example e = new SubClass();//subclass object can be assigned to the abstract class reference
    e.method();
   System.out.println(i);
}
}


Note : 

Concrete method - method with method body
Abstract method -  method without method body. Method body will be given for sub class implementation.
Instance variable - are the variables available for the class object.



Abstract method: 

     Abstract method is a method without method body. "abstract" is the key word used to declare a abstract method. Abstract method body should be implemented in the class that extends it. Abstract method will be present in the abstract class.

Example:

public abstract class Example{

public abstract void abstractmethod(); //This is a abstract method without method body.
}
     

Abstract Class:

    Abstract class is not a complete class, its incomplete. We cannot create object for the Abstract class since its incomplete. The Abstract class can be extended by a sub class where all the abstract methods need to be implemented. Abstract class contains concrete methods, abstract method and instance variables.

 We can create object for the  sub class  which extends the Abstract class. Since the sub class is a complete class that has the implementation of the abstract methods. Subclass object can be given to the Abstract class reference.

Example:

/*
*Abstract class
*/
public abstract class Example {
static int i=10;//instance variables
public abstract void method(); //abstract method
public void add(){
   System.out.println("Concrete method");
 }
}

/*
*Sub class that extends the abstract class
*/
public class SubClass extends Example {

public void method(){
   System.out.println("This is a method implementation of the abstract method");
}

public static void main(String args[]){

   Example e = new SubClass();//subclass object can be assigned to the abstract class reference
    e.method();
   System.out.println(i);
}
}


Note : 

Concrete method - method with method body
Abstract method -  method without method body. Method body will be given for sub class implementation.
Instance variable - are the variables available for the class object.