Thursday, March 26, 2015

JVM (Java Virtual Machine)

1- What is JVM

- JVM = Java Virtual Machine

It is software component that provide runtime environment for java byte code to be executed. JVM is available on many hardware and software platforms.
Its implementation is know as JRE (Java Runtime Environment) and when ever user write java command on the command prompt to run the java class, and instance of JVM is created.

It performs operation like Load code, Verifies Code, Execute Code.



Now Lets discuss about few main point on JVM Architecture:-





1) Classloader:

Load class in to this area so that it can be executed in JRE.

2) Class(Method) Area:

It store stores code for methods.

3) Heap:

It is the runtime data area in which objects are allocated.

4) Stack:

Java Stack stores reference variable.

5) Program Counter Register:

It contains the address of the Java virtual machine instruction currently being executed.

6) Native Method Stack:

It contains all the native methods used in the application.

7) Execution Engine:

It contains virtural processor, Interepreter that read our *.class byte file and JIT-Just-In-Time(JIT) compiler :- Actually this is used to improve the performace in JAVA. Our *.class byte code is fragmented into many small segment and all the similar component is compiled only once at run time. Compile means translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


Wednesday, March 25, 2015

JAVA Class Loader

Applications written programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. But in case of java specific application this is different. JAVA is dynamically compiled programming languages. In Java, compiler generate the .class files and it remain as-is until loaded into the Java Virtual Machine (JVM) . Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.

Lets try to understand using our famous HelloWorld

public class HelloWorld {
   public static void main(String argv[]) {
      System.out.println("Hello World");
   }
}
If you run this class specifying the -verbose:class command-line option, so that it prints what classes are being loaded

java -verbose:class HelloWorld



[Opened C:\Program Files\Java\jre1.5.0\lib\rt.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jsse.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jce.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\charsets.jar]
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
---
---
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded HelloWorld from
Hello World
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
As you can see, the Java runtime classes required by the application class (HelloWorld) are loaded first.

How to add external JS in GWT Project

Let say your GWT project name is barchart


Add following line in BarChart.html

Create a public folder and keep all your *.js file in it. Make sure to have same flow structure of packages.




Folder wise screen shot





How to add Fusion chart js to gwt project

Add following line to your Project.html





Add following line to your Project.html



Tuesday, February 12, 2013

Different ways to create objects in Java


There are four different waysto create objects in java:

1. Using new keyword
This is the most common way to create an object in java.

MyObject object = new MyObject();

2. Using Class.forName()


MyObject object = (MyObject) Class.forName("com.MyTest").newInstance();

 3. Using clone()

The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

4. Using object deserialization

Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
Now you know how to create an object. But its advised to create objects only when it is necessary to do so.

5.Using Reflection

this.getClass().getClassLoader().loadClass("com.MyTest").newInstance();

Annotation concept in JAVA

Annotation is a concept introduced by J2SE 5 and above it that allows programmers to add additional information called metadata into a Java source file. Annotations do not change the execution of a program but the information embedded using annotations can be used by various tools during development and deployment.


Annotation is similar to creating an interface. Having declaration preceded by an @ symbol. The @Retention annotation is used to specify the retention policy, i.e. SOURCE, CLASS, or RUNTIME.

RetentionPolicy.SOURCE retains an annotation only in the source file and discards it during compilation.
RetentionPolicy.CLASS stores the annotation in the .class file but does not make it available during runtime.
RetentionPolicy.RUNTIME stores the annotation in the .class file and also makes it available during runtime.


There are two part for Annotation

1) Annotation Type
2) Annotation itself


1) Annotation Type

package hello;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String MyAuthor();    // Annotation member
    String MyDate();      // Annotation member
}

2) Annotation itself

package hello;

import java.lang.reflect.Method;

@MyAnnotation(MyAuthor="Siddhu",MyDate="12/2/2013,12/02/2013")
public class TestMyAnnotation {
@MyAnnotation(MyAuthor="Siddhu",MyDate="12/02/2013")
   public static void myMethod()
   {
       System.out.println("Hi .......... inside the method");      
   }
public static void showMyAnnotations()
{
TestMyAnnotation test=new TestMyAnnotation();
   try
   {
    Class c=test.getClass();
       Method m=c.getMethod("myMethod");
       MyAnnotation annotation1=(MyAnnotation)c.getAnnotation(MyAnnotation.class);
       MyAnnotation annotation2=m.getAnnotation(MyAnnotation.class);
       System.out.println("Author for class: "+annotation1.MyAuthor());
       System.out.println("Date for  class: "+annotation1.MyDate());
       System.out.println("Author for method: "+annotation2.MyAuthor());
       System.out.println("Date for method: "+annotation2.MyDate());
   }
   catch(NoSuchMethodException ex)
   {
       System.out.println("Invalid Method..."+ex.getMessage());
   }
}
   public static void main(String args[])
   {
       myMethod();
       showMyAnnotations();
   }
}


OutPut:

Hi .......... inside the method
Author of the class: Siddhu
Date of Writing the class: 12/2/2013,12/02/2013
Author of the method: Siddhu
Date of Writing the method: 12/02/2013

Wednesday, August 01, 2012

Concept of Connection Pool using JAVA Code


class MypoolCountMgr
{

 String databaseUrl = "jdbc:oracle://IpAddresss:XXXX/myDatabase";
 String userName = "UN";
 String password = "UP";

 Vector poolCount = new Vector();

 public MypoolCountMgr()
 {
  initialize();
 }

 public MypoolCountMgr(
  String databaseUrl,
  String userName,
  String password
  )
 {
  this.databaseUrl = databaseUrl;
  this.userName = userName;
  this.password = password;
  initialize();
 }

 private void initialize()
 {
  //initialize
  initializepoolCount();
 }

 private void initializepoolCount()
 {
  while(!checkIfpoolCountIsFull())
  {
   System.out.println("Connection is available. We are proceeding to add new connections");
   //Adding new connection
   poolCount.addElement(createNewConnectionForPool());
  }
  System.out.println("Connection Pool is full.");
 }

 private synchronized boolean checkIfpoolCountIsFull()
 {
  final int MAX_POOL_SIZE = 5;

  //Check if the pool size
  if(poolCount.size() < 5)
  {
   return false;
  }

  return true;
 }

 //Normal Code to Creating a new connection
 private Connection createNewConnectionForPool()
 {
  Connection connection = null;

  try
  {
   Class.forName(DRIVER_TOLOAD);
   connection = DriverManager.getConnection(databaseUrl, userName, password);
   System.out.println("Connection: "+connection);
  }
  catch(SQLException sqle)
  {
   System.err.println("SQLException: "+sqle);
   return null;
  }
  catch(ClassNotFoundException cnfe)
  {
   System.err.println("ClassNotFoundException: "+cnfe);
   return null;
  }

  return connection;
 }

 public synchronized Connection getConnectionFromPool()
 {
  Connection connection = null;

  //Check if there is a connection available
  if(poolCount.size() > 0)
  {
   connection = (Connection) poolCount.firstElement();
   poolCount.removeElementAt(0);
  }
    return connection;
 }

 public synchronized void returnConnectionToPool(Connection connection)
 {
  //Adding the connection from the client back to the connection pool
  poolCount.addElement(connection);
 }

 public static void main(String args[])
 {
  MypoolCountMgr MypoolCountMgr = new MypoolCountMgr();
 }

}