Serialisation in java

By | April 20, 2014

Serialization is one of the most commonly asked topics in technical interviews.Specially if you are from java background.In this post I’ll try to address most of the questions which you may encounter in an interview. I’ve explained serialization in detail in this article but if you are looking for something specific you can directly jump to that seaction:
1.What is Serialization in java?
2.How to Serialize and Deserialize an object in java?
3.What is Serialization in java?
4.What is transient keyword and its purpose ?

What is Serialization in java?
Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.After a serialized object has been written into a file, it can be read from the file and deserialized. The type information and bytes that represent the object and its data can be used to recreate the object in memory.It’s should be noted that this entire process is JVM independent.Means you can serialize an object on one platform and deserialize it on other.

How to Serialize and Deserialize an object in java?
Classes ObjectInputStream and ObjectOutputStream contain the methods for serializing and deserializing an object.
The ObjectOutputStream class contains many write methods for writing various data types.One of the method which needs special mention here is

public final void writeObject(Object x) throws IOException

This method serializes an Object and sends it to the output stream.

Similarly, in ObjectInputStream we have the following method for deserializing an object:

public final Object readObject() throws IOException,ClassNotFoundException

This method retrieves the next Object from the stream and deserializes it. The return value is Object, so you have need to cast it to its appropriate data type.

Below I’ve written a piece of code to demonstrate how Serialization and most of surrounding concepts works in java.
But first something important from java documentation:

Serializability of a class is enabled by the class implementing the interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Person class, which implements the Serializable interface:

Notice that for a class to be serialized successfully, two conditions must be met:
The class must implement the interface.
All of the fields in the class must be serializable. If any field is not serializable, it must be marked transient.

The following SerializePerson program instantiates an Employee object and serializes it to a file person.ser .

Now the code to Deserialize ,The following DeserializePerson program deserializes the Person object created above.

What’s the significance of SerialVersionUID?
Try deleting this line private static final long serialVersionUID = 1L; form Person class above and you will get this warning
The serializable class Person does not declare a static final serialVersionUID field of type long.
The serialVersionUID is used as a version control in a Serializable class.JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. If you do not explicitly declare a serialVersionUID, JVM will do it for you automatically, based on various aspects of your Serializable class.The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the deserialization process.Click this link serialVersionUID to read a detailed post on serialVersionUID.

What is transient keyword and its purpose ?
If a variable is declared as transient, then it will not be persisted. It is the main purpose of the transient keyword.For example in the Person class above the field age is declared as transient .So it won’t be serialized.
Clearly we can get the age of Person if we have serialized the birthYear of Person object.In short we declare a member variable as transient if we don’t need it or we can get it’s value by some other means.

One thought on “Serialisation in java

Leave a Reply

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