CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Serialization in Java

Have you ever thought of storing the object information into a file or stream and retrieve it back when required? Java Serialization allows us to do this. Serialization is the process of converting a Java object into a stream of bytes. And deserialization is the reverse process, where the byte stream is converted to recreate the actual object in memory.

The Serialization process preserves the state of an object when it travels through different networks. The byte stream that is created is not JVM dependent, that is, it is platform-independent and hence the Java object can be serialized on one platform and can be de-serialized on a different platform.

How to implement Serialization in Java?

There are few important things we need to remember, to implement serialization in Java. Here are those;

  • To be able to serialize an object, its class needs to implement the java.io.Serializable interface.
  • The writeObject() method from the ObjectOutputStream class then serializes the object by converting it into a byte stream and stores it in an output stream (usually a file).
  • The readObject() method from the ObjectInputStream class de-serializes the object by converting the byte stream stored in the file into the actual object.

The ObjectOutputStream Class

This class writes the Java objects to an output stream (usually a file) in the form of a sequence of bytes. To be able to write objects to streams, their respective classes should implement the Serializable interface.

We can use public ObjectOutputStream(OutputStream os) throws IOException {} construction, to create an output stream for storing the byte stream. Here is it looks like;

ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("file.txt"));

Once we have the object, we can use it’s methods writeObject to writes the object into the stream; flush() method to flushes the current output stream. Finally we can use, close() method to close the stream.

The ObjectInputStream Class

Above class we used to store the information of the object; where as this class to de-serializes the object by converting the byte stream back into the actual Java object.

We use public ObjectInputStream(InputStream is) throws IOException {} constructor to creates the specified input stream. Here is the object creation looks like;

ObjectInputStream is=new ObjectInputStream(new FileInputStream("file.txt"));

readObject() method is used to read the object information from the input stream. We need to type cast the object to it’s original data type.

Person p2 = (Person) instream.readObject();

close()method we use to closes the input stream.

The java.io.Serializable Interface

The Serializable interface is termed as a marker interface, that is, it does not contain any data member or method.

It’s only use is, to ‘mark’ the Java classes as serializable, which indicates that the objects of the serializable classes are eligible to be converted into byte streams and undergo serialization.

Here are the things we must have to remember;

  • Only non-static members of the object undergo serialization and their data is saved since static data members belong to the class and not the object.
  • Transient data members of the object are also not serialized, therefore if we don’t want to save the data of any data member then we can mark it as static or transient.
  • For transient data member, the value will be reset to its default value after deserialization and in case of static data member the state of the data member will not be saved, that is, if the value of the data member is changed after the serialization, then after de-serializing, the current value (i.e., the changed value) will be taken.

Here is an example of Serialization

Let’s see an example depicting how the objects can be serialized:

Run the program and see the results, how it looks like;

$javac SerializationDemo.java

$java -Xmx128M -Xms16M SerializationDemo

The Object after serialization and before deserialisation
Number of Wheels = 16
Vehicle Type = Cargo
Vehicle Ref. Number = 7373
Width = 40
Height = 22

The Object after deserialization
Number of Wheels = 16
Vehicle Type = Cargo
Vehicle Ref. Number = 7897
Width = 0
Height = 0

In the above example, while deserializing, the object read from the input stream should be type casted before assigning it to the class instance.

We will discuss more topics in upcoming Articles.

Serialization in Java

Leave a Reply

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

Scroll to top