Interview Questions
Java
java Advanced Interview questions
Java Freshers Interview Questions
Java interview questions
Java Interview Questions
Java Serialization Advanced Interview Questions
In this post, we will see Serialization advanced interview questions. In this you might see some tricky questions as well
1. What is the difference between the Serializable and Externalizable interfaces in Java?
2. How do you handle large object graphs during serialization in Java?
- Use the
transient
keyword to exclude fields from serialization: - Use the
static
keyword to exclude class fields from serialization: - Use a custom serialization mechanism: By implementing the
Externalizable
interface, the class can define its own serialization mechanism using thewriteExternal()
andreadExternal()
methods, which can improve performance and reduce the amount of memory used. - Use object compression: By compressing the serialized data, the size of the data can be reduced, which can improve performance and reduce the amount of memory used. Java provides
DeflaterOutputStream
andInflaterInputStream
classes for this purpose. - Use serialization proxy pattern
- circular references: If the object graph contains circular references, the serialization process may not terminate and an exception may be thrown. You can use
writeObject(Object obj, ObjectStreamClass desc)
andreadObject()
methods to handle circular references
3. Can an inner class be serializable?
- An inner class in Java can be serialized, but with certain limitations.
- Inner classes, like any other class, can implement the
Serializable
interface and be serialized using theObjectOutputStream
andObjectInputStream
classes. - If inner class is defined as non-static inner class (also known as an inner class or a member class), it has an implicit reference to the enclosing class, which means that the enclosing class will also be serialized along with the inner class. This can result in the serialization of a large number of unnecessary objects and can cause performance issues.
- it is recommended to use a static nested class instead of a non-static inner class to avoid any issues
- A static nested class does not have an implicit reference to the enclosing class and will not cause the enclosing class to be serialized along with it.
4. Can Local class be serializable?
Local inner classes are not serializable as they are not declared with any name and they don’t have any state that can be saved.
5. Can Anonymous Inner class serializable ?
Anonymous inner classes are not serializable as they are not declared with any name and they don’t have any state that can be saved.
6. How does serialization handle enum type fields
Please click on below link to see the answer: https://www.javasavvy.com/how-to-serialize-and-deserialize-enums/
7. How does default serialization mechanism in Java handle object references
- Java’s serialization mechanism handles object references in such a way that object graph is correctly reconstructed during deserialization.
- When an object is serialized, the
ObjectOutputStream
checks if the object has been previously serialized. If it has, it writes a reference to the previously serialized object to the output stream, instead of writing the entire object again. This is called “object replacement.” - If the object has not been previously serialized, the
ObjectOutputStream
writes the object’s class information and the object’s data to the output stream. The object is then added to a table of objects that have been serialized, so that it can be referenced if it is encountered again during the serialization of other objects. - During deserialization, the
ObjectInputStream
reads the class information and the data for an object from the input stream, and creates a new instance of the object. - If the object was previously serialized, the
ObjectInputStream
uses the reference to the previously serialized object instead of creating a new instance. - This mechanism ensures that the object graph is correctly reconstructed during deserialization and ensure objects are not duplicated.
- It also ensures that circular references between objects are handled correctly.
- Finally, this mechanism ensures that the object graph is correctly reconstructed during deserialization and that objects are not duplicated.
- serialization handles object references by writing a reference to previously serialized objects to the output stream, instead of writing the entire object again,
- deserialization handles by using the reference to previously serialized objects instead of creating a new instance of the object.
Thanks for Reading…
Comments are closed.