Top Java Serialization frequently asked interview questions
In this post we will look at Java Serialization Interview questions. Examples are provided with explanation.
Q: What is Serialization?
A: Serialization can be defined as the process to make the object's state persistent. TO achieve this the state of the object is written into byte stream. This is used usually when we need to save the object in database using JPA, sending object to JMS etc. Serialization is implemented using writeObject() method ObjectOutputStream.
Q: What is Deserialization?
A: Deserialization is the process of retrieving the objects state from the byte stream. It is the oppposite of Serialization. Deserialization is implemented using readObject() method ObjectOutputStream
Q: What is a serialVersionUID and why should I use it?
A: Each serializable class has a version numberber associated with it, this is called serialVersionUID. A serializable class declares it's own serialVersionUID by declaring a field named serialVersionUID which must be static, final, and of type long. The main objective of using serialVersionUID is to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If this is not the case then an exception of type InvalidClassException.
Q: What is the difference between Serialization and Marshaling?
A: Marshalling is the process of converting an object into JSON/XML to get parameters, while serialization is the process of converting an object to byte stream to save it's state.