Java Serialization Questions


Java Serialization Interview Questions

  1. Explain java serialization.

  2. What is the purpose of serialization?

  3. Explain Deserialization.

  4. What are Serialization and Deserialization conceptually?

  5. Why do we mark a data member transient?

  6. Is it allowed to mark a method as transient?

  7. How does marking a field as transient makes it possible to serialize an object?

  8. What is an Externalizable interface in Java?

  9. Difference between a Serializable and an Externalizable interface?


1- It is a process of converting an object into a byte array. This byte array represents the class, version, and internal state of the object. JVM can use this byte array to transmit/read the object over a network.

2- Some of the uses of serialization are:

  • Communication: It is used for transmitting an object over a network between two machines.

  • Cross JVM Synchronization: It can be used in the same way across multiple JVM that follow different architecture.

  • Caching: Serialization can be used for caching to improve performance. We may need 10 minutes to build an object, but it may take just 10 seconds to de-serialize the object.

  • Persistence: We can store the object’s state in a database and retrieve it from the database later on.

3- It is the process of reconstructing the object from the serialized state. It is the reverse process of serialization.

4- Serialization is to convert Object data into a stream of bytes. Deserialization is to convert a stream of bytes back into a copy of the original object.

5- Member variables of an object are marked transient to indicate that they should not be serialized.

6- No, Java does not allow marking a method as transient. It is valid only for member variables.

7- Let say we have a class ABC that implements a Serializable interface, but it contains a member variable object of class XYZ that does not implement Serializable interface. Due to this, it is not possible to Serialize the class ABC. To solve this issue, we can mark the member variable XYZ as Transient in class ABC. This will allow us to serialize the class ABC.

8- Externalizable interface extends Serializable interface in Java. It is used for giving the Class control over saving and restoring the contents of its instances. A class implements methods writeExternal() and readExternal() to  store and restore the object.

9- Serializable is a marker interface and Externalizable is not a marker interface.

In Serialization, we need to define serialVersionUID. If it is not explicitly defined it will be generated automatically based on all the fields, methods of the class. When we implement the Serializable interface, the class is serialized automatically by default. We can override writeObject() and readObject()methods to control more complex object Serialization process. The serializable interface is based on the recursive algorithm. Serializable gives you two options. One option is to provide a custom way of serialization, the other default way. In Externalizable, you have to always implement readExternal() and writeExternal() methods. A public no-arg constructor is needed while using the Externalizable interface. In the case of Externalizable, we use readExternal() and writeExternal() methods to give control to class for class’s serialization process.


Check Interview Questions on Java

Wikipedia Java

Leave a Comment

Your email address will not be published.