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?
Could not find what you were looking for? send us the question and we would be happy to answer your question.

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 sent over network to other JVM.

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.

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.

4. Why do we need Externalizable interface?

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

5. Is Serializable a marker interface in Java?

Yes. It has no methods.

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.

7. Which variables are not serialized during Java Serialization?

Static and transient variables cannot be serialized.

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 returns a hash value.The serialVersionUID is also called suid.

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

No. Serializable interface is a marker interface.

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.

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.

12. Is Externalizable a marker interface in Java?

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

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.

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.

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.

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 expensive.
  • Serialize attributes only with NON-default values.
  • Use Externalizable interface and implement the readExternal and writeExternal methods to dynamically identify the attributes to be serialized.
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.

18. List few alternatives to Java serialization.

Saving object state to database using ORM tools,

Xml based data transfer,

and JSON Data Transfer.

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.

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 serialized object stream.

Changing a transient field to a non-transient field, static to non-static are compatible changes.

Adding or removing writeObject()/readObject() methods.

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 modify these methods, though adding or removing is a compatible change.

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 object we call ObjectInputStream.readObject() method.

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.

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 soft cache for future use.

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.

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 generated serial version ID.

Assign your own value and postfix with 'L'.

private static final long serialVersionUID = 19L;

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

SerializableExternalizable
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 methods, we are defining them in our class. Override writeExternal() and readExternal() for serialization process to happen when implementing Externalizable interface.
This is a marker interface, does not have any methods.This has 2 methods readExternal and writeExternal, hence it is not a marker interface.
Constructor is not called during deSerialization.Constructor is called during deSerialization.

28. Do primitive data types involve in serialization?

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

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 will fail by throwing java.io.InvalidClassException.

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.

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 deserialized. This method is called when ObjectInputStream has read an object from the stream and is preparing to return it to the caller.

For serializable objects, the readObject method allows a class to control the deserialization of its own fields and held responsible for restoring the state of the class.

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.

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.

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.

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 transient. Eliminate unwanted properties to be serialized. Save only the state of the object, not the derived attributes.
  • Serialize attributes only with NON-default values.
  • Use Externalizable interface and implement the readExternal and writeExternal methods to dynamically identify the attributes to be serialized.
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 object in the stream to designate the object to be returned. The object returned should be of a type that is compatible with all uses. If it is not compatible, a ClassCastException will be thrown when the type mismatch is discovered.

«
»
JDBC

Comments & Discussions