Difference Between Serializable and Externalizable in Java Serialization
The process of writing the state of an object to a file is called serialization, but strictly speaking, it is the process of converting an object from java supported form into a file-supported form or network-supported form by using fileOutputStream and objectOutputStream classes we can implement serialization.
But we can serialize only serializable objects. An object is said to be serializable if and only if the corresponding class implements a Serializable interface. Serializable interface is present in java.io package, and it doesn’t contain any method and hence it is a marker interface. If we are trying to serializable a non-serializable object then we will get Runtime Exception saying notSerializableException.
Object has been serialized Object has been deserialized Name:Ram Age:34 2364
In serialization, everything is taken care of by JVM and the programmer doesn’t have any control. In serialization, it is always possible to solve the total object to file, and it is not possible to save part of the object which may create performance problems. To overcome this problem we should go for externalization.
The main advantage of Externalizable over serialization is, everything is taken care of by the programmer and JVM doesn’t have any control. Based on our requirements we can save either the total object or part of the object which improves the performance of the system. To provide the generalizable ability for any java object, it’s a mandate for the corresponding class to implement a generalizable interface.
This interface defines two methods as follows:
public void writeExternal( ObjectOutput obj ) throws IOException
This method will be executed automatically at the time of serialization within this method we have to write code to save the required variable to the file.
public void readExternal(ObjectInput in )throws IOException , ClassNotFoundException
This method will be executed automatically at the time of deserialization. Within this method, we have to write code to read the required variable from the file and assign it to the current object. But strictly speaking at the time of deserialization JVM will create a separate new object by executing a public no-argument constructor and then on that object, JVM will call the readExternal method.
Hence, every Externalizable implemented class should compulsorily contain a public no-argument constructor otherwise we will get Runtime Exception saying InvalidClassException.
Public no-argument constructor Name : Ram Age : 35
Now, we are done discussing both of them, let us conclude all the differences in a table for a better reach of understanding.
|A serializable interface is used to implement serialization.||An externalizable interface used to implement Externalization|
|Serializable is a marker interface i.e. it does not contain any method.||The externalizable interface is not a marker interface and thus it defines two methods writeExternal() and readExternal().|
|Serializable interface passes the responsibility of serialization to JVM and the programmer has no control over serialization, and it is a default algorithm.||The externalizable interface provides all serialization responsibilities to a programmer and hence JVM has no control over serialization.|
|Serialization using a serializable interface has bad performance.||Serialization using an externalizable interface has better performance.|
|Default serialization does not require any no-arg constructor.||A public no-arg constructor is required while using an Externalizable interface.|
|It is hard to analyze and modify class structure because any change in structure may break serialization.||It is relatively easy to analyze and modify class structure because of complete control over serialization logic.|
|Using a serializable interface we save the total object to a file, and it is not possible to save part of the object.||Base on our requirements we can save either the total object or part of the object.|
|Transient keywords play an important role here.||Transient keywords won’t play any role.|