Saturday, July 3, 2021

What is default Serialization in Java? Serializable and Externalizable Interface Explanation

What is Serialization in Java
Serialization in Java is a process to persist any Java Object's state into a File System or convert them into a byte stream to transfer over the network to another JVM or program. Serialization in Java is done by JVM by employing the default Serialization process which persists all of the Object's state except the transient variable and a static variable. How Serialization works in Java is another popular Java Serialization interview question, well It's easy to make any Java class Serializable, the class needs to implements interface, and JVM will automatically serialize its instance when passed over to using writeObject()

Serializable interface is also called marker interface or tag interface because it does not contain any method or behavior and just used to tag a class, by seeing a class implementing Serializable JVM knows that it needs to serialize an instance of this class.

Default Serialization in Java

We can customize the default Serialization process by using the interface. The difference between Serializable and Externalizable in Java is another important question related to the Java Serialization process. In fact, it's advised that not to use the default serialization process as it's very fragile and use the custom binary form for your objects, and implement Serialization using interface.

One example of how fragile the default Serialization process is SerialVersionUID. SerialVersionUID is a unique id associated with a serialized form of class and it depends on upon class's structure. 

If you implement an interface, SerialVersionUID generated by the Default Serialization process would be different, which means you can not deserialize the class with the new code. This can be avoided by declaring a long constant called serialversionUID in the class itself.

Another intrinsic cost of Serialization is maintenance. The serialized form of a class is part of its public API and once you commit that you need to support it a life long otherwise you won't be able to load serialized instances by old code. This can be minimized by using a custom Serialize form.

That's all on what is Serialization in Java. Serialization is the core of Java RMI protocol and one of the popular techniques of storing Object' state in Java. It's a complex topic for many Java Programmers, especially beginners who struggle to understand how Serliazation works, how to save and restore an object's state, and how to customize the serialization process in Java. 

Even experienced Java programmers struggle to do it correctly and if you are one of them then I highly recommend reading Serialization chapters on Effective Java, the classical Java book which I think every Java developer should read. 

Those chapters explain the whole serialization process in detail, you will learn about small details like how the readObject and writeObject method works, how you can avoid creating duplicate objects using Serialization, and how to keep Singleton constraint valid while saving and restoring the state of a Java object which implements singleton pattenr. 

No comments:

Post a Comment
