Prev Next

Java / Java Serialization

1. What is Serialization in Java? 2. How to create a serializable class in Java? 3. Difference between Serializable and Externalizable interface in Java. 4. Why do we need Externalizable interface? 5. Is Serializable a marker interface in Java? 6. Can a Serialized object be transmitted through network? 7. Which variables are not serialized during Java Serialization? 8. What is serialVersionUID in Java? 9. Do we need to implement any method when using Serializable interface? 10. What is transient variable in Java? 11. Why static variables are not serialized in Java? 12. Is Externalizable a marker interface in Java? 13. What happens when one of the members of the class does not implement Serializable interface? 14. How to prevent a child class from being serialized when it's parent class already implements Serializable interface? 15. What are the compatible and incompatible changes in Java Serialization Mechanism? 16. How to improve Java serialization performance? 17. When to use Serializable versus Externalizable? 18. List few alternatives to Java serialization. 19. How do I serialize a collection in Java? 20. Give more examples of compatible changes in Java serialization. 21. Give few examples of incompatible changes in Java serialization. 22. Which method is used during Serialization and DeSerialization process in Java? 23. What is the value of a transient variable after de-serialization? 24. Does having SerialVersionUID variable improve Java serialization performance? 25. Is the constructor invoked when a Java object is de-serialized? 26. How to generate a SerialVersionUID in Java? 27. List few differences between Serializable and Externalizable in Java. 28. Do primitive data types involve in serialization? 29. What happens when a class does not define SerialVersionUID in Java? 30. Is constructor of parent class called during DeSerialization process of child class? 31. Difference between readResolve and readObject methods in Java serialization. 32. How do I prevent deserialization process creating another instance of Singleton class? 33. Is there any limit on the size of a serialized object? 34. How do I serialize a third party library class in Java? 35. How do I improve Serialization performance? 36. When is the readResolve method called?

1. What is Serialization in Java?

Java serialization is the process by which Java objects are serialized by storing object's state into a file with extension .ser. Restoring object's state from that file is called deserialization. Object Serialization converts Java object into a binary format which can be persisted to disk or sen...

Read full answer

2. How to create a serializable class in Java?

Implement java.io.Serializable interface in the Java class and JVM will allow to serialize its objects in default serialization format.

Read full answer

3. Difference between Serializable and Externalizable interface in Java.

Externalizable provides writeExternal() and readExternal() methods which gives flexibility to override java serialization mechanism instead of using on default serialization.

Read full answer

4. Why do we need Externalizable interface?

Externalizable Interface allows us to control serialization mechanism which may help improve application performance.

Read full answer

5. Is Serializable a marker interface in Java?

Yes. It has no methods.

Read full answer

6. Can a Serialized object be transmitted through network?

Yes, a Serialized object can be transmitted via the network as Java serialized object remains in form of bytes. Serialized objects can also be stored in Disk or database as Blob.

Read full answer

7. Which variables are not serialized during Java Serialization?

Static and transient variables cannot be serialized.

Read full answer

8. What is serialVersionUID in Java?

Every time an object is serialized the Java serialization mechanism automatically computes a hash value called serialVersionUID. ObjectStreamClass's computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which ret...

Read full answer

9. Do we need to implement any method when using Serializable interface?

No. Serializable interface is a marker interface.

Read full answer

10. What is transient variable in Java?

Transient variables are not included in the serialization and are not the part of the object's serialized state. A transient variable can be created by specifying transient keyword.

Read full answer

11. Why static variables are not serialized in Java?

The static variables are class level variables and are not the part of the object state so they are not saved as the part of serialized object.

Read full answer

12. Is Externalizable a marker interface in Java?

No. It has two methods readExternal and writeExternal to be implemented.

Read full answer

13. What happens when one of the members of the class does not implement Serializable interface?

At runtime NotSerializableException is thrown when try to serialize the class when one of the members does not implement serializable interface.

Read full answer

14. How to prevent a child class from being serialized when it's parent class already implements Serializable interface?

When the parent implements serializable then the child Class also be serializable although it doesn't implement Serializable interface. As a workaround, implement writeObject() and readObject() method in the child class and throw NotSerializableException from those methods.

Read full answer

15. What are the compatible and incompatible changes in Java Serialization Mechanism?

Adding new field or method is a compatible change and changing class hierarchy or UN-implementing Serializable interface are non compatible changes.

Read full answer

16. How to improve Java serialization performance?

Java serialzation performance directly rely on the number and size of attributes in the Java object. To improve the performance, Mark the unwanted or non Serializable attributes as transient. Save only the state of the object, not the derived attributes.Some times, serializing them can be expensi...

Read full answer

17. When to use Serializable versus Externalizable?

Use of transient keyword enables selective attribute serialization, however, use of Externalizable interface can be really effective in some cases when you have to serialize only some dynamically selected attributes of a large object.

Read full answer

18. List few alternatives to Java serialization.

Saving object state to database using ORM tools, Xml based data transfer, and JSON Data Transfer.

Read full answer

19. How do I serialize a collection in Java?

All standard implementations of collections List, Set and Map interface already implement java.io.Serializable. However ensure all the objects added in collection are Serializable as well.

Read full answer

20. Give more examples of compatible changes in Java serialization.

Adding a new field will not affect serialization. The newly added field will be set to its default values when the object of an older version of the class is unmarshaled. Changes In access modifiers such as private, public, protected or default is compatible since they are not reflected in the se...

Read full answer

21. Give few examples of incompatible changes in Java serialization.

Changing implementation from Serializable to Externalizable interface, Deleting an existing Serializable field, Changing non-transient field to transient, non-static to static, Changing the field type, Updating the class package. Modifying the writeObject() / readObject() method, we must not modi...

Read full answer

22. Which method is used during Serialization and DeSerialization process in Java?

Java Serialization is done by java.io.ObjectOutputStream class, a filter stream which is wrapped around a lower-level byte stream to handle the serialization mechanism. To store any object via serialization mechanism we call ObjectOutputStream. writeObject (saveThisobject) and to deserialize that...

Read full answer

23. What is the value of a transient variable after de-serialization?

It will be set to its default value. For example, an int transient variable will be set to zero.

Read full answer

24. Does having SerialVersionUID variable improve Java serialization performance?

Declaring a serialVersionUID field in a Java class saves CPU time the first time the JVM process serializes a given Class. However the performance gain is not very significant, In case when you have not declared the serialVersionUID its value is computed by JVM once and subsequently kept in a sof...

Read full answer

25. Is the constructor invoked when a Java object is de-serialized?

If the class implements Serializable , the constructor is not called during deserialization process. However, if the class implements Externalizable , the constructor is called during deserialization process.

Read full answer

26. How to generate a SerialVersionUID in Java?

There are 3 ways to create a serialVersionUID value. Using serialVer command bundled with JDK, pass the serializable class name as command parameter to get its version identifier. Using Eclipse IDE , hover at the class and from the context menu choose Add default serial version ID, or Add generat...

Read full answer

27. List few differences between Serializable and Externalizable in Java.

Serializable Externalizable Serializable has its own default serialization process, we just need to implement Serializable interface. We can customize default serialization process by defining following methods in our class, readObject() and writeObject(). Note: We are not overriding these method...

Read full answer

28. Do primitive data types involve in serialization?

Yes, all the primitive data types are part of serialization.

Read full answer

29. What happens when a class does not define SerialVersionUID in Java?

If the serialVersionUID is not defined, then after any modification made in class, we won?t be able to deSerialize existing objects for same class because serialVersionUID generated by Java compiler for the modified class will be different from the old serialized object. Deserialization process w...

Read full answer

30. Is constructor of parent class called during DeSerialization process of child class?

If superclass implements Serializable - constructor is not called while if the superclass doesn't implement Serializable - constructor is called during DeSerialization process of child class.

Read full answer

31. Difference between readResolve and readObject methods in Java serialization.

For Serializable and Externalizable classes, the readResolve method allows a class to replace/resolve the object read from the stream before it is returned to the caller. By implementing the readResolve method, a class can directly control the types and instances of its own instances being deseri...

Read full answer

32. How do I prevent deserialization process creating another instance of Singleton class?

Use readResolve method to return the same instance of a class, rather than creating a new instance.

Read full answer

33. Is there any limit on the size of a serialized object?

Yes, it is limited by the amount of memory your JVM can allocate to the creation and maintenance of your object. As for writing it out to disk, it is limited by the maximum file size of the underlying OS. Linux, as an example, has a 2GB limit on one file.

Read full answer

34. How do I serialize a third party library class in Java?

If the third party class is not final you could just extend it , have that implement Serializable and write your own writeObject and readObject methods.

Read full answer

35. How do I improve Serialization performance?

The serialization process performance heavily depends on the number and size of attributes of the object to be serialized. Below are some tips to speed up the marshaling and un-marshaling of objects during Java serialization process. Mark the unnecessary or non Serializable attributes as transien...

Read full answer

36. When is the readResolve method called?

The readResolve method is called when ObjectInputStream has read an object from the stream and is preparing to return it to the caller. ObjectInputStream checks whether the class of the object defines the readResolve method. If the method is defined, the readResolve method is called to allow the ...

Read full answer

«
»

Comments & Discussions