Serialization and Deserialization in java

In java serialization is way used to convert an object into a byte stream which can be transported to any other running JVM through a network or can be persisted into disk and that object can be rebuilt again. Java provides serialization API for this.

How to make a class serializable in java?

To make a java class Serializable implements java.io.Serializable interface. JVM will take care of serializing objects.

What is Serializable interface?

Serializable interface is a marker interface. A marker interface doesn’t have any method i.e. Serializable interface doesn’t have any method, it gives indication to compiler that use Java Serialization mechanism to serialize this object.

Example:

SerializationExample.java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/**
 * This program is used to show the serialization process.
 * @author w3schools
 */
class Student implements Serializable{
	//Serial Version UID.
	private static final long serialVersionUID = 1L;
	String name;
	String className;
	String rollNo;
 
	//Constructor.
	Student(String name, String className, String rollNo){
		this.name = name;
		this.className = className;
		this.rollNo = rollNo;
	}
}
 
class Test{
	//Write serialized object into objectoutputstream.
	public void objectSerialization(Student stu){
	   try
	      {
	         //Creating FileOutputStream object.
	         FileOutputStream fos = 
	          new FileOutputStream("F:\\New folder\\student.ser");
 
	         //Creating ObjectOutputStream object.
	         ObjectOutputStream oos = new ObjectOutputStream(fos);
 
	         //write object.
	         oos.writeObject(stu);
 
	         //close streams.
	         oos.close();
	         fos.close();
 
	         System.out.println("Serialized data is saved in " +
	         		"F:\\New folder\\student.ser");
	      }catch(IOException e)
	      {
	          System.out.println(e);
	      }
	}
}
 
public class SerializationExample {
	public static void main(String args[]){
		//Creating Student object.
		Student stu = 
                 new Student("Parmander", "MCA", "MCA/07/27");		
		//Creating Test object.
		Test obj = new Test();		
		//Method call.
		obj.objectSerialization(stu);		
	}
}

Output:

Serialized data is saved in F:\New folder\student.ser

Download this example.

Deserialization:

Deserialization is the reverse process of serialization. It is the process of rebuilding object from serialized state.

Example:

DeSerializationExample.java

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
 
/**
 * This program is used to show the deserialization process.
 * @author w3schools
 */
class Student implements Serializable{
	//Serial Version UID.
	private static final long serialVersionUID = 1L;
	String name;
	String className;
	String rollNo;
 
	//Constructor.
	Student(String name, String className, String rollNo){
		this.name = name;
		this.className = className;
		this.rollNo = rollNo;
	}
}
 
class Test{
	//Deserialize a serialize object.
	public void objectDeSerialization(){
	  try
	      {
		Student stu = null;
		//Creating FileOutputStream object.
		FileInputStream fis = 
	         new FileInputStream("F:\\New folder\\student.ser");
 
	         //Creating ObjectOutputStream object.
	         ObjectInputStream ois = new ObjectInputStream(fis);
 
	         //write object.
	         stu = (Student) ois.readObject();
 
	         //close streams.
	         ois.close();
	         fis.close();
 
	         System.out.println("Name = " + stu.name);
	         System.out.println("Class Name = " + stu.className);
	         System.out.println("RollNo = " + stu.rollNo);
	      }catch(Exception e)
	      {
	          System.out.println(e);
	      }
	}
}
 
public class DeSerializationExample {
	public static void main(String args[]){	
		//Creating Test object.
		Test obj = new Test();		
		//Method call.
		obj.objectDeSerialization();		
	}
}

Output:

Name = Parmander
Class Name = MCA
RollNo = MCA/07/27

Download this example.

What is the difference between Serializable and Externalizable interface in Java?

                     Serializable                           Externalizable
  1. Serializable interface is a marker interface.
  2. Serializable interface write state of an object in non-compress format.
  1. Externalizable interface is not a marker interface.
  2. Externalizable interface writes state of an object in compress format.

 
Next Topic: Transient in java with example.
Previous Topic: How To Check If A File Exists In Java.