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:

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 Hence, we need to import packege*.
                 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. 


class Sample implements Serializable
int i;
int j;

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

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

         /* 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);

parag@parag-Inspiron-N4010:~/Desktop/prog$ javac 
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: 

Check other posts on Multi-Threading in this link

No comments:

Post a Comment