Java

What Is Java Serialization?

Here, we’ll talk about object serialization and de-serialization as well as their purposes.

What is Java Serialization?

In order to write an object into a file, send it over a network, or store it in a database, it must first be serialized into a stream of bytes. The opposite of serialization is de-serialization. Simply put, serialization and de-serialization involve converting an object to a stream of bytes and reconstructing the object from the stream of bytes, respectively. The features for performing serialization and de-serialization are provided by the Java Serialiation API. For a class to be eligible for serialization, it must implement the java.io.Serializable interface.

Let’s use an illustration to help you better grasp the ideas:

Example
This class can be serialized because it implements the Serializable interface. Except for the fields that are declared transient, all the fields of this class can be written to a file after being converted to streams of bytes. Two transient fields are present in the example below; these fields will not participate in serialization.
Student.java

public class Student implements java.io.Serializable{
  private int stuRollNum;
  private int stuAge;
  private String stuName;
  private transient String stuAddress;
  private transient int stuHeight;
 
  public Student(int roll, int age, String name,
  String address, int height) {
    this.stuRollNum = roll;
    this.stuAge = age;
    this.stuName = name;
    this.stuAddress = address;
    this.stuHeight = height;
  }
 
  public int getStuRollNum() {
    return stuRollNum;
  }
  public void setStuRollNum(int stuRollNum) {
    this.stuRollNum = stuRollNum;
  }
  public int getStuAge() {
    return stuAge;
  }
  public void setStuAge(int stuAge) {
    this.stuAge = stuAge;
  }
  public String getStuName() {
    return stuName;
  }
  public void setStuName(String stuName) {
    this.stuName = stuName;
  }
  public String getStuAddress() {
    return stuAddress;
  }
  public void setStuAddress(String stuAddress) {
    this.stuAddress = stuAddress;
  }
  public int getStuHeight() {
    return stuHeight;
  }
  public void setStuHeight(int stuHeight) {
    this.stuHeight = stuHeight;
  }
}

Serialization of Object

A Student class object is being written by this class to the Student.ser file. To write the object to File, we are utilizing the FileOutputStream and ObjectOutputStream.

Note: The file name should have a.ser extension in accordance with Java Serialization best practices.

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
public class SendClass
{
  public static void main(String args[])
  {
    Student obj = new Student(101, 25, "Chaitanya", "Agra", 6);
    try{ 
      FileOutputStream fos = new FileOutputStream("Student.ser"); 
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(obj);
      oos.close();
      fos.close();
      System.out.println("Serialzation Done!!");
   }catch(IOException ioe){
      System.out.println(ioe);
    }
  }
}

Output:

Serialzation Done!!

De-serialization of Object

After reading the stream of bytes from the file, this class would rebuild the Student class object. See how the student address and student height variables have sequential null and 0 values in the output of this class. This is due to the Student class’s declaration that certain fields are transitory.

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class AcceptClass {

 public static void main(String args[])
 {
    Student o=null;
    try{
      FileInputStream fis = new FileInputStream("Student.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      o = (Student)ois.readObject();
      ois.close();
      fis.close();
    }
    catch(IOException ioe)
    {
       ioe.printStackTrace();
       return;
    }catch(ClassNotFoundException cnfe)
     {
       System.out.println("Student Class is not found.");
       cnfe.printStackTrace();
       return;
     }
    System.out.println("Student Name:"+o.getStuName());
    System.out.println("Student Age:"+o.getStuAge());
    System.out.println("Student Roll No:"+o.getStuRollNum());
    System.out.println("Student Address:"+o.getStuAddress());
    System.out.println("Student Height:"+o.getStuHeight());
 }
}

Output:

Student Name:Chaitanya
Student Age:25
Student Roll No:101
Student Address:null
Student Height:0

 

 

 

 

 

 

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button