Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, 17 June 2016

Java Environment Setup

The reason behind adding the path to environment variable is that you need not go to java executables path again n again while compiling and running java files.  

Download Java.

Setting environment variable is not compulsory, but it is recommended.

If you do not set the PATH variable, you need to give full path of executables everytime you run it. such as:


C:\Java\jdk1.7.0\bin\javac ClassName.java


How to setup:

Right click on my computer.

Select Property option.

Now from property select Advance system Setting option.

click on new button and variable name.



Now go to path variable under system variable menu.

and add JAVA_HOME variable to path like as follows.

%JAVA_HOME%\bin



Click ok and close all windows.

To test the settings open command prompt and type 
java -version and hit enter.










Serializing and Deserializing in java

Serializing: Serialization is the mechanism to write the object into byte Stream, this mechanism used while dealing with object sending on remote clients using such client/server Java technologies as RMI or socket programming.

Basically, it converts all data into the byte stream, it contains data of the object as well as information about the object like type, metadata etc.


All the wrapper classes implement java.io.Serializable interface by default.


java.io.Serializable interface :

Serializable is interface has no body, it is basically used to mark java classes which support certain capabilities.

One advantage of serialization is that it compresses the data of the object to make travel of object across the network easy.

It has Following Methods:

1) public final void writeObject(Object obj) throws IOException {}writes the object to the ObjectOutputStream.
2) public void flush() throws IOException {}flushes the existing output stream.
3) public void close() throws IOException {}closes the output stream.

Here is the Example:

We are going to Serialize object of Schools in NACC class.

import java.io.Serializable
public class Schools implements Serializable{
       int s_Id;
       String s_Name;
       
       public Schools(int s_Id, String s_Name){      //We are creating constructor 
               this.s_Id = s_Id;
               this.s_Name=s_Name;
       }
}


NACC Class:


import java.io.*;  

class NACC{  

  public static void main(String args[])throws Exception{  

  Schools    s =new Schools(211,"MIT");  

  
  FileOutputStream fout=new FileOutputStream("SchoolsObject.txt");  
  ObjectOutputStream out=new ObjectOutputStream(fout);  
  
  out.writeObject(s);  
  out.flush();  
  System.out.println("Object Serialized.");  
 }  
}  

Here what is did here!

1) It creates one text file using FileOutputStream.

2) It Converts Object of Schools class into byte Stream using class

    ObjectoutputStream.
    If you open that text file, it will not be in human readable format.



3) We wrote data of the Object using out.writeObject() method.






Deserializing:
Deserializing means re-creating serialized object from serialized state.

It is the reverse process of serializing, here we convert byte stream data into the object again to make it operational.

We do this by using ObjectinputStream class.

Important Method :

1) public final Object readObject() throws IOException, ClassNotFoundException{}reads an object from the input stream.
2) public void close() throws IOException {}closes ObjectInputStream.


Example of Deserialization:

import java.io.*;
class Depersist{
 public static void main(String args[])throws Exception{
   
    ObjectInputStream in=new ObjectInputStream(new                    FileInputStream("SchoolObject.txt"));
    Schools s=(Schools)in.readObject();
    System.out.println(s.s_Id+" "+s.s_Name);
 
     in.close();
 }
}  


ObjectInputStream  gets the bytes converted in object from given file and then
It casts converted Object into Schools type.

readObject() function then reads the object and print function simply prints it.

   



Happy Learning.........