Difference between Serializable and Externalizable in Java

In this tutorial,  we are going to see differences between Serializable and Externalizable interface in Java.

Serializable :

It is marker interface and you do not have to provide any methods if you implement this interface. When if any class implement Serializable interface, JVM will take care of Serializing that object.


As the name suggest it is externalizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshaling and unmarshalling of objects.Externalizable interface extends Serializable interface. If you implement this interface, you need to provide implementation of readExternal() and writeExternal() method.

Externalizable vs Serializable: 

Marker interface
It is marker interface. You don't have to provide implementation of any method.
Externalizable is not marker interface, you have to override writeExternal and readExternal method.
Serializable interface has less control over serialization process and it is optional to override readObject and writeObject.
Externalizable interface has more control over serialization process and it is mandatory to override writeExternal and readExternal.
JVM uses reflection to perform serialization in the case of Serializable interface which is quite slow.
Programmer have to implement readExternal and writeExternal methods but it relatively results in better performance
If you implement Externalizable interface and provide implementation of readExternal and writeExternal then it supersedes readObject and writeObject methods in that class. It is due to the fact that Externalizable extends Serializable interface.
Constructor called during Deserialization
Default constructor is not called during Deserialization process.

Default constructor is called during Deserialization process.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012