Sunday 18 December 2016

Difference between Constructors and Static block


1. Constructor - Called when we create object for the class
    Static block - Called when the class is loaded in the memory even before the constructor.

2. Constructor - Name of the constructor should be same as the class name.
     Static block - No name is there for static block. Only static{} keyword is used for the declaration.

3. Constructor - Multiple constructor with different signatures can be present in same class.
     Static block - There can be many static block in a class.
Constuctor in Java:

A constructor should have same name as the class without any return type.

The constructor is called immediately after the object creation for the class.

The constructor is a block of code used to instantiate the variables in the class.

Constructor can be created with parameters and without parameters.

There can be more that one constructor for a single class as we do for method overloading with different signatures.

It is a block of code that gets executed during the object creation, but this executes after the static block. (Static block gets executed immediately when the class is loaded in memory)


Example for constructor:


public class constructorExample {
//static block
static{
System.out.println("Static block is called when the class is loaded in the memory.");
}
    //Constructor without parameter
public constructorExample() {
System.out.println("Const with no args");
}
//Constructor with paramerter
public constructorExample(String name) {
System.out.println("Name: " + name);
}
public static void main(String args[]) {
constructorExample ce = new constructorExample();
constructorExample ce1 = new constructorExample("Karthikeyan");
}


}



Output:

Static block is called when the class is loaded in the memory.
Const with no args
Name: Karthikeyan

Wednesday 19 October 2016

Classes and Objects in Java:

Object in general terms is anything that physically exist in the world.
Class is a blueprint of the object.

Here is a example below which gives you a easy hint to understand Class and Object.

Example: Flower is a generic term that does not exist in the world. Rose, lotus..etc only exist in the world which is physically present and that is the object. Here the term flower is the class.

Class contains member variables and methods. This can be accessed only with the help of object.

Object is the instance of the class.
Object creation only will allocate memory for the class members.
Object is also called as instance.

Example of the class:

class ExampleClass{
  public String name = "Sam";
}

class Caller{
 public static void main(String args[]){
    ExampleClass ec = new ExampleClass();//Object is created for the class
    System.out.println(ec.name);//Member of the class is access with the object
 }
}

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.



Thursday 30 June 2016

JVM, JDK and JRE

To develop java application you need to download java exe from online and install in your machine.Download from the below location and install it in your machine.

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html


Java contains JDK and JRE in it.


Expansion of the terms:


JVM - Java Virtual Machine

JDK - Java Development Kit

JRE - Java Runtime Environment

Content in Java:

1) JRE, JVM and JDK
2) Java Memory Management
3) Classes and Object
4) Constructor
5) Static Block 
6) Primitive data types
7) Object Oriented Programming Language
  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
8) Interface
9) Abstract Class
10) Exception Handling
11) Collection Framework
  • Set
  • List
  • Queue
  • Map
12) Threads
13) Serilization
14) Wrapper Classes

Wednesday 29 June 2016

JAVA 


Java was developed by James Gosling and his friends. They released it in 1995 as a Core Component of Sun Micro systems. The main reason to develop Java is to overcome the issues faced by the C language. C is platform dependent which means it should be executed in the same platform in which you develop.

JDK(Java Development Kit) 1.0 released in 23rd Jan 1996. It became a highly used language in the industry. In towards world Java is used  nearly 90% the application that exists it the world.

 Current Java version is 8.

Advantages of Java over C language:

Java,

1) It is platform independent.
2) It doesn't use pointers which is a tough concept used in C language.
3) It takes care of the Memory management.
4) It's a object oriented programming.
5) It is highly secure.