Mohd Toufeeq khan
Mohd Toufeeq khan

Reputation: 45

Importance of Serialization and Deserialization

Can anybody tell or share some useful links regarding the importance of Serialization and Deserialization?. I am not able to understand that, is it just to transfer object across network or store them into files we use Serialization and deserialization or any other aspect involve?

Upvotes: 3

Views: 1115

Answers (2)

Mithun Debnath
Mithun Debnath

Reputation: 588

You can check this Source

Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.

Note − When serializing an object to a file, the standard convention in Java is to give the file a .ser extension. Example to Serialize

import java.io.*;
public class SerializeDemo {



public static void main(String [] args) {
  Employee e = new Employee();
  e.name = "Reyan Ali";
  e.address = "Phokka Kuan, Ambehta Peer";
  e.SSN = 11122333;
  e.number = 101;

  try {
     FileOutputStream fileOut =
     new FileOutputStream("/tmp/employee.ser");
     ObjectOutputStream out = new ObjectOutputStream(fileOut);
     out.writeObject(e);
     out.close();
     fileOut.close();
     System.out.printf("Serialized data is saved in /tmp/employee.ser");
  }catch(IOException i) {
     i.printStackTrace();
  }


 }
}

Example to Deserialize

import java.io.*;
public class DeserializeDemo {




public static void main(String [] args) {
  Employee e = null;
  try {
     FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
     ObjectInputStream in = new ObjectInputStream(fileIn);
     e = (Employee) in.readObject();
     in.close();
     fileIn.close();
  }catch(IOException i) {
     i.printStackTrace();
     return;
  }catch(ClassNotFoundException c) {
     System.out.println("Employee class not found");
     c.printStackTrace();
     return;
  }

  System.out.println("Deserialized Employee...");
  System.out.println("Name: " + e.name);
  System.out.println("Address: " + e.address);
  System.out.println("SSN: " + e.SSN);
  System.out.println("Number: " + e.number);


}
}

the output should be like

Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101

Upvotes: 1

Eugene
Eugene

Reputation: 121008

Serialization is the process of transforming your instance to bytes. Now what you do with those bytes is completely up to you.

Hibernate uses Serialization for detached entities for example.

You can also pass some bytes (your instance) for inter-process communication, etc.

Upvotes: 1

Related Questions