Thursday, June 2, 2016

Serialization & Externalization in Java

java.io.Serializable interface

Serializable 

Serializable Interface is a Marker Interface. Hence there is no method in Serializable interface.Serialization is a process in which state of Object will be saved into a byte stream(binary format) which can be persisted into disk or sent over network to any other running JVM.
It is mainly used in Hibernate, RMI, JPA technologies.

The reverse operation of serialization is called deserialization.
The String class and all the wrapper classes implements java.io.Serializable interface by default.It must be implemented by the class whose object you want to persist.

Advantage of Java Serialization

It is mainly used to sent object's state on the network (known as marshaling).
Byte stream can be used for different purpose.
1.Write to Disk
2.Store in Memory
3.Sent byte stream over network to other platform
4.Save byte stream in DB(As BLOB)



Example:

    import java.io.*; 
    class Persist{ 
     public static void main(String args[])throws Exception{ 
      Student s1 =new Student(211,"ravi"); 
     
      FileOutputStream fout=new FileOutputStream("f.txt"); 
      ObjectOutputStream out=new ObjectOutputStream(fout); 
     
      out.writeObject(s1); 
      out.flush(); 
      System.out.println("success"); 
     } 
    } 
   
Output: success

Java Serialization with array or collection

Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed.


Externalizable:

Syntax :
public interface Externalizable extends Serializable

It is not a marker interface.As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of objects.Externalizable interface extends Serializable interface.
If you implement this interface then you need to override following methods.

    @Override
    public void readExternal(ObjectInput arg0) throws IOException,

            ClassNotFoundException {

    }

    @Override
    public void writeExternal(ObjectOutput arg0) throws IOException {

    }

Now lets see how serialization happens:

At sender side:
JVM checks if class implements externalizable or not.If it does then serialize object using writeExternal() method.
If it does not implement externalizable but implements serializable , object is serialized using ObjectOutputStream.

At receiver side:
When object is reconstructed and it is externalizable , an instance is created using no args constructor and
readExternal is called.If it is not externalizable but serializable , object is reconstructed using ObjectInputStream.

The Externalizable interface provides two methods:
public void writeExternal(ObjectOutput out) throws IOException
public void readExternal(ObjectInput in) throws IOException



Difference between serialization and externalization
Externalizable provides us writeExternal() and readExternal() method which
gives us flexibility to control java serialization
mechanism instead of relying on Java's default serialization.


Can you Customize Serialization process or can you override default Serialization process in Java?
ObjectOutputStream.writeObject (saveThisobject) is invoked and for reading object ObjectInputStream.readObject()
is invoked but there is one more thing which JVM provides you is to define these two method in your class.
If you define these two methods in your class then JVM will invoke these two methods instead of applying default serialization mechanism.


Which kind of variables is not serialized during Java Serialization?
This question asked sometime differently but the purpose is same whether Java developer knows specifics about static and transient variable or not.Since static variables belong to the class and not to an object they are not the part of
the state of object so they are not saved during Java Serialization process.
As Java Serialization only persist state of object and not object itself. Transient variables are also not included in java serialization process and are not  the part of the object’s serialized state.

Read More: https://www.journaldev.com/2452/serialization-in-java

Referred Sites :

1.http://www.javatpoint.com/serialization-in-java
2.http://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.html#ixzz4AQVlkNqa
3.http://www.java2blog.com/2014/02/externalizable-in-java.html

No comments:

Post a Comment