Serialization Interview Questions

What is Serialization?
  • Serializable is a marker interface.

  • When an object has to be transferred over a network (typically through rmi or EJB) or to persist the state of an object to a file, the 'object' Class needs to implement 'Serializable' interface.

  • Implementing this interface will allow the object to be converted into bytestream and transfer over a network.

What is the use of serialVersionUID?
  • During object serialization, the default Java serialization mechanism writes the metadata about the object, which includes the class name, field names, types and superclass.

  • This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type.

  • Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass's computeSerialVersionUID() method passes the class name, sorted member names, modifiers and interfaces to the secure hash algorithm (SHA), which returns a hash value.

  • The 'serialVersionUID' is also called 'suid'. So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not 'InvalidClassException' is thrown.

Changes to a serializable class can be compatible or incompatible. Following is the list of changes which are compatible:

  • Add fields

  • Change a field from static to non-static

  • Change a field from transient to non-transient

  • Add classes to the object tree

List of incompatible changes:

  • Delete fields

  • Change class hierarchy

  • Change non-static to static

  • Change non-transient to transient

  • Change type of a primitive field

So, if no suid is present, inspite of making compatible changes, jvm generates new suid thus resulting in an exception if prior release version object is used.

The only way to get rid of the exception is to recompile and deploy the application again.

If we explicitly mention the sUid using the statement:

    private final static long serialVersionUID

then if any of the mentioned compatible changes are made the class need not be recompiled. But for incompatible changes there is no other way than to compile again.

Learn in Detail about Java Programming

What is the need of serialization?

The serialization is used:

  • To send state of one or more object's state over the network through a socket.

  • To save the state of an object in a file.

  • An object's state needs to be manipulated as a stream of bytes.

Other than serialization what are the other different approaches to make an object serializable?

Besides the Serializable interface, at least three other alternate approaches can serialize Java objects:

  • For object serialization, instead of implementing the Serializable interface, a developer can implement the 'Externalizable' interface, which extends Serializable. By implementing Externalizable, a developer is responsible for implementing the 'writeExternal()' and 'readExternal()' methods. As a result, a developer has sole control over reading and writing the serialized objects.

  • 'XML' serialization is an often-used approach for data interchange. This approach lags runtime performance when compared with Java serialization, both in terms of the size of the object and the processing time. With a speedier XML parser, the performance gap with respect to the processing time narrows. Nonetheless, XML serialization provides a more malleable solution when faced with changes in the serializable object.

  • Finally, consider a 'roll-your-own' serialization approach. You can write an object's content directly via either the 'ObjectOutputStream' or the 'DataOutputStream'. While this approach is more involved in its initial implementation, it offers the greatest flexibility and extensibility. In addition, this approach provides a performance advantage over Java serialization.

Do we need to implement any method of 'Serializable' interface to make an object serializable?

No. Serializable is a Marker Interface. It does not have any methods.

What happens if the object to be serialized includes the references to other serializable objects?

If the object to be serialized includes references to the other objects, then all those object's state will also be saved as the part of the serialized state of the object in question. The whole object graph of the object to be serialized will be saved during serialization automatically provided all the objects included in the object's graph are serializable.

What happens if an object is serializable but it includes a reference to a non-serializable object?

If you try to serialize an object of a class which implements serializable, but the object includes a reference to a non-serializable class then a 'NotSerializableException' will be thrown at runtime.

On execution of above code following exception will be thrown:

java.io.NotSerializableException: NonSerial_JBK at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java)

Are the static variables saved as the part of serialization?

No. The static variables belonging to the class are not the part of the state of an object so they are not saved as the part of serialized object.

What is a transient variable?

These variables are not included in the process of serialization and are not the part of the object's serialized state.

What will be the value of transient variable after de-serialization?

The value of transient variable after de-serialization will be its default value. Say for instance if the transient variable in question is an 'int', its value after deserialization will be zero.

Result of the above piece of code is:

Explanation for the same is as follows:

The transient variable is not saved as the part of the state of the serialized variable, it's value after de-serialization is it's 'default' value.

Does the order in which the value of the transient variables and the state of the object using the 'defaultWriteObject()' method saved during serialization matter?

Yes, while restoring the object's state the transient variables and the serializable variables that are stored must be restored in the same order in which they were saved.

Learn about Serialization in our Video Tutorial