Wednesday 2 August 2017

Serialization in Java with example

                    In this post, we will see Serialization in Java with Example | Serialization and Deserialization in Java with Example | serialization in java,serialization and deserialization in java,serialization,serialization in java example,serialization and deserialization in java with example.

Watch following video:




Watch on YouTube: https://www.youtube.com/watch?v=GJTxQBU0iPg

What is Serialization?
                    The process of translating Object State into a format which can be stored in file or memory buffer is called Serialization. We can also transmit Object State across network by this process.
                    Now, what is Object State? Object is an instance of a class. i.e. every object of class have their dedicated variables. At a particular time instance, values allocated to the variables of any object is called State of that object. Saving an Object State in the form of stream of bytes into file or memory buffer is called Serialization.

What is the need of Serialization?
                    Serialization supports persistence of data; in the sense, object state which is saved can be used later on without any change. 
                    Serialization is useful when we have to transmit object over the network.
                     Serialization is important for detecting the changes in data over the time period. This means, we can can save the data at different interval of time and can compare in between them.

What is Deserialization?
                      Reversal process of serialization is called Deserialization i.e. retrieving the object state from file or memory buffer is called Deserialization.
                 
How to achieve Serialization?
                      In Java, we can achieve Serialization by using ObjectOutputStream Class while we can achieve Deserialization by using ObjectInputStream class. 
                      Storing an object state in Java is considered to be bad convention. Hence, in Java, we have to mark the Class (whose objects has to be serialized) as Serializable. We can do this by implementing interface Serializable.
                     We can find above mentioned classes and interface in package java.io. Hence, we need to import packege java.io.*.
                      
Example:
                 Go through the following example:
                 Here we are creating an object of Sample class. We are allocating the values to the variables i and j. We are saving them into a text file as byte stream and retrieving back from the same file.

SerializationExample.java 

 import java.io.*;

class Sample implements Serializable
{
int i;
int j;
}

public class SerializationExample
{
  
    public static void main(String args[]) throws Exception
      {
         Sample obj=new Sample();
         obj.i=5;
         obj.j=11;
         
         File f=new File("sam.txt");

         /* Serialization */
         FileOutputStream fos=new FileOutputStream(f);
         ObjectOutputStream oos=new ObjectOutputStream(fos);
         oos.writeObject(obj);

         /* Deserialization */
         FileInputStream fis=new FileInputStream(f);
         ObjectInputStream ois=new ObjectInputStream(fis);
         Sample obj1=(Sample)ois.readObject();
         
         System.out.println("Values retrived from file are "+obj1.i+" and "+obj1.j);
         
     }
  
}
    
  

Output:
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac SerializationExample.java 
parag@parag-Inspiron-N4010:~/Desktop/prog$ java SerializationExample

Values retrived from file are 5 and 11







Check how to send serialized object over network in java in following post:
https://www.comrevo.com/2019/07/Sending-objects-over-sockets-Java-example-How-to-send-serialized-object-over-network-in-Java.html 

Check other posts on Multi-Threading in this link http://www.comrevo.com/2016/09/multi-threading.html


No comments:

Post a Comment