Wednesday 25 September 2013

JVM Architecture

In JVM we have three layers, they are:
1. Class loader sub system
It is a part of JVM which loads the class files into main memory of the computer.
While generating class files by the JVM it checks for existence of java API which we are using
as a part of java program. It the java API is not found it generates compilation error.
2. Runtime data area:
When the class files are loaded into main memory JVM requires the following
runtime data area, they are
Heap memory: It is used for allocating the memory space for
at runtime.
Java stacks/method area
amount of memory space in the main memory on stack memory known as method area.
PC register: PC stands for Program Counter which i
PC register always gives address of next instruction of java program to the JVM.

JVM Architecture


3. Execute engine:
When a JVM is compiling in a java program in that context JVM is known as
compiler. When a JVM is running a java program in that context JVM is known as interpreter.
JVM reads the byte code of
instructions which are used by JVM for generating the result of the java programs.
Method interface in th

JVM (execution engine) and java API. Java API contains collection of packages.

Tuesday 6 March 2012

Create Empty DOM Document


package com.usr.xml.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class CreateEmptyDOMDocument {
public static void main(String[] args) {
       System.out.println("creating empty DOM Document");
       try{
              DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
              DocumentBuilder db=dbf.newDocumentBuilder();
              Document doc=db.newDocument();
       }
       catch(Exception ex){
              ex.printStackTrace();
       }
       System.out.println("empty DOM Document created");
}
}

java program to read properties xml file

userinfo.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>User Info</comment>
<entry key="age">38</entry>
<entry key="password">india@123</entry>
<entry key="username">sachin</entry>
</properties>


package com.usr.properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class ReadPropertiesXmlFile {
       public static void main(String[] args) {
              Properties pr = new Properties();
              try {
                     InputStream is = new FileInputStream("userinfo.xml");
                     pr.loadFromXML(is);
                     is.close();

                     Enumeration enumKeys = pr.keys();
                     while (enumKeys.hasMoreElements()) {
                           String key = (String) enumKeys.nextElement();
                           String value = pr.getProperty(key);
                           System.out.println(key + ":" + value);
                     }
              } catch (FileNotFoundException ex) {
                     ex.printStackTrace();
              } catch (IOException ex) {
                     ex.printStackTrace();
              }
       }
}
OUTPUT
age:38
password:india@123
username:sachin
 

java program to convert properties file into xml file

userinfo.properties

username=sachin
password=india@123
age=38

package com.usr.properties;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesToXML {
       public static void main(String[] args) {
              File file=new File("userinfo.properties");
              try{
                     FileInputStream fis=new FileInputStream(file);
                     BufferedReader br=new BufferedReader(new InputStreamReader(fis));
                     Properties pr=new Properties();
                     pr.load(br);
                     OutputStream os=new FileOutputStream("userinfo.xml");
                     pr.storeToXML(os,"User Info", "UTF-8");
                     System.out.println("converted to xml");
                     br.close();
                     fis.close();
                     os.close();
              }
              catch(FileNotFoundException ex){
                     ex.printStackTrace();
              }
              catch (IOException ex) {
                     ex.printStackTrace();
              }

       }
}

OUTPUT
userinfo.xml 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>User Info</comment>
<entry key="age">38</entry>
<entry key="password">india@123</entry>
<entry key="username">sachin</entry>
</properties>


Saturday 3 March 2012

java program to find second largest number in array


package com.usr.arrays;

public class SecondLargestNumberInArray {
       public static void main(String[] args) {
              int arr[] = { 1, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85,
                           49, 53, 56, 61, 65, 84 };
              secondLargeNumber(arr);
       }

       public static void secondLargeNumber(int[] arr) {
              int largest = arr[0];
              int secondLargest = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (arr[i] > largest) {
                           secondLargest = largest;
                           largest = arr[i];
                         
                     } else if (arr[i] > secondLargest) {
                           secondLargest = arr[i];
                         
                     }
              }
              System.out.println("second largest in array is:" + secondLargest);

       }
}

Saturday 25 February 2012

How to create immutable classes in java


Immutable class is the class whose value cannot be changed throughout the life cycle.
Ex:String
We cannot change the content of String class, everytime new reference is created when we change the content.

To create custom immutable  class in java
  1. Class should be made final so that no class can extend it. 
  2. Declare all variables as final
  3. Provide constructors to set the values
  4.  There should not be any public set method which can change the state of 
    the object.
  5. provde getter method




package com.usr.general;

final class Person {
       final int age;
       final String name;

       public Person(int age, String name) {
              this.age = age;
              this.name = name;

       }

       public int getAge() {
              return age;
       }

       public String getName() {
              return name;
       }
}

public class ImmutableDemo {
       public static void main(String[] args) {
              Person p = new Person(38, "sachin");
              System.out.println(p.age);
              System.out.println(p.name);
       }
}

OUTPUT
38
sachin