Skip to main content

What is serialization and deserialization in Java?

Serialization in Java is a process of writing the state (in other words, the value of its instance variables) of an object into a special type of byte stream.
In real word it is mainly used in Hibernate, RMI, JMS and EJB technologies.

Deserialization is the reverse process of serialization where byte stream converted into a java object.

Serialization and deserialization process is JVM independent, it means you can serialize an object in a JVM and deserialize in another JVM.

serialization in java

Why is serialization required?

  1. It is used for write the object state into file for future use.
  2. To send and object over a network (known as marshaling).
marshaling in java

Key point’s need to be remember

  1. All class whose object we want to serialize must be implements Serializable interface.
  2. All the wrapper classes and String class implements the java.io.Serializable interface by default.
  3. Serializable is a marker interface. The main property of the marker interface is, it has no any variable and method.
    It is only used to “mark” Java classes so that JVM provides some more capability to the objects of these classes.
  4. Static data members will not be serialized because static is the part of class not object.
  5. Suppose we don’t want to serialize instance (non-static) data member then make it transient.

1- Serialization example in java

Customer.java

import java.io.Serializable;

public class Customer implements Serializable {

	private static final long serialVersionUID = 1L;
	private long customerId;
	private String name;

	public Customer(long customerId, String name) {
		this.customerId = customerId;
		this.name = name;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[customerId=").append(customerId).append(", name=").append(name).append("]");
		return builder.toString();
	}
}

SerializeTest.java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeTest {

	public static void main(String[] args) {
		try {
			// Create an object
			Customer customer = new Customer(101L, "Rahul sharma");
			FileOutputStream fout = new FileOutputStream("customer.txt");
			ObjectOutputStream out = new ObjectOutputStream(fout);
			out.writeObject(customer);
			out.flush();
			// Close the stream
			out.close();
			System.out.println("Successfully serialized.");
		} catch (IOException e) {
			System.out.println(e);
		}
	}
}

Output:

Successfully serialized.

2- Deserialization example in java

DeSerializeTest.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeSerializeTest {

	public static void main(String[] args) {
		try {
			// Create an object
			FileInputStream fin = new FileInputStream("customer.txt");
			ObjectInputStream in = new ObjectInputStream(fin);
			Customer customer = (Customer) in.readObject();
			// Close the stream
			in.close();
			System.out.println(customer);
		} catch (IOException | ClassNotFoundException e) {
			System.out.println(e);
		}
	}
}

Output:

[customerId=101, name=Rahul sharma]

Leave a Reply

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

Top