Serialization and Deserialization in Java With Examples

Java Serialization

Java serialization is the process that is used to write the state of an object into a byte stream. EJB, RMI, JMS, and RMI are some of the technologies that use serialization. Deserialization does exactly the opposite of what serialization does.

The benefit of Serialization

The main function of serialization in Java is to travel the state of an object on the network. This process is also called marshaling.

Java.io.Serializable interface

In Java, the serializable interface does not have any data member and method. It is also called a marker interface because it marks the classes of Java in order to provide a specific capability to the objects of the classes.

Other marker interfaces include the Cloneable and the Remote interfaces. The classes whose objects the programmer want to persist implement the serializable class. It is implemented by default in all the wrapper classes and the string classes.

Example:

ObjectOutputStream class

In Java, the ObjectOutputStream class writes the primitive data types and the objects to an output stream. Such objects that support the serializable interface can only be written to the stream.

Constructor

  • public ObjectOutputStream(OutputStream out) throws IOException {}

This constructor is used to construct an ObjectOutputStream which writes to a particular output stream.

Methods used

The following are the methods that are used in the ObjectOutputStream class:

  • public final void writeObject(Object obj) throws IOException {}

This method is used to write a particular object to the ObjectOutputStream.

  • public void flush() throws IOException {}

This method is used to flush the present output stream.

  • public void close() throws IOException {}

This method is used to close the present output stream.

Example:  

 

Deserialization in java

In Java, deserialization is the mechanism that reconstructs the object from a serialized state. It does exactly the opposite of what serialization does.

ObjectInputStream class

In Java, the main function of the ObjectInputStream class is to deserialize objects and primitive data written through the ObjectOutputStream.

Constructor

  • public ObjectInputStream(InputStream in) throws IOException {}

This constructor is used to construct an ObjectInputStream which reads from a particular input stream.

Methods used

The following are the methods that are used in the ObjectInputStream class:

  • public final Object readObject():

This method is used to read an object from the input stream.

  • public void close() throws IOException {}

This method is used to close the ObjectInputStream.

Java Serialization with Inheritance (IS-A Relationship)

The subclasses of a class that has implemented the serializable interface are all serializable.

Java Serialization with Aggregation (HAS-A Relationship)

The serialization process is not possible in the situation in which a class has a reference to another class and the references have not been serialized.

Java Serialization with the static data member

The class which has a static data member in it cannot be serialized. This is because static is not a part of an object but it is a part of a class.

Java Serialization with array or collection

The serialization process is not possible in the situation in which an array or collection has not been serialized.

Example: 

Leave a Reply

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