Wednesday 25 July 2018


Spring REST VS Jersey(JAX-RS implementation):

REST API creation
  • Spring REST provides RESTFul implementation along with the ability to create a  Web full stack development support.
  • Whereas JAX-RS (Jersey, RESTEasy and Apache CXF implementation ) provide REST implementation only.


JAX-RS compliance
  • Spring REST is not in compliance with JAX-RS.
  • JAX-RS is a JSR Specification defining a Java API for RESTful Web Services.

-          Jersey, RESTEasy and Apache CXF are the implementation of JAX-RS.
-          We can use Jersey implementation if we go with JAX-RS compliance.

Server Migration
  • JAX-RS ensures portability of REST API code across all Java EE compliant application servers.
  • Spring REST we need to check whether we will have any issues in the server migration.


JAX-RS - Java API for RESTful WebService:

JAX-RS is a Java Specification Request (JSR).

JAX-RS ensures portability of REST API code across all Java-EE compliant application servers.

Many used to compare JAX-RS with Jersey and they think both are implementation for RESTful web service, that is not correct.

To make thinks clear, Jersey is an implementation of JAX-RS.

Other implementation that are JAX-RS compliant are RESTEasy and Apache CXF .




Tuesday 21 February 2017

Primitive Data Types:

Primitive data types are the basic data types available in Java. They are given below

Types  - Declaration
int a =1;
float a =1.111111;
long a= 123333;
double a = 1234.123456789123;

Wednesday 15 February 2017

Scanner and BufferedReader:

Scanner and BufferedReader are used to get input from user using STD INPUT.
The syntax and examples are given below, I will explain the concept in my upcoming post.

Scanner syntax : 

Scanner sc = new Scanner(System.in);

BufferedReader syntax  :        

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  

Program to Understand Scanner and BufferedReader:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class inputoutput {
public static void main(String[] args) {
scannerInput();
bufferedSample();
}
public static void bufferedSample() {
System.out.println(":::Example for BufferedReader:::");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter your Name: ");
String name = br.readLine();
System.out.println("Enter your Age :");
int age = Integer.parseInt(br.readLine());
System.out.println("Enter your Sex :");
char sex = (char) br.read();
System.out.println("Name: " + name + "\n" + "Age: " + age + "\n" + "Sex: " + sex);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void scannerInput() {
System.out.println(":::::Example with Scanner::::");
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Name:");
String name = sc.next();
System.out.println("Enter an Age: ");
int age = sc.nextInt();
System.out.println("Enter the Sex: ");
char sex = sc.next().charAt(0);
System.out.println("Name :" + name);
System.out.println("Age :" + age);
System.out.println("Sex :" + sex);
}

}

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.