Difference between Serializable and Externalizable in Java Serialization? Answer (original) (raw)
The difference between serializable and externalizable is the popular java interview question which we have touched on in my earlier post on Serialization: Top 10 java serialization interview questions. knowing differences between externalizable and serializable is not just important from the interview point of view but also getting control of the serialization process and optimizing the performance of serialization. both serializable and externalizable are used to serialize or persist java objects but the way they do is a little different.
In the case of Serializable Java Virtual machine has full control for serializing objects while in the case of Externalizable, the application gets control for persisting objects.
Both writeExternal() and readExternal() method provides complete control on the format and content of the Serialization process to the application which can be leveraged to increase the performance and speed of the serialization process.
And, If you are a beginner then I also recommend you to join these free Java Programming courses to learn Java in a better and more structured way. These are the best and up-to-date free courses to learn Java online.
Serialization and Externalization in Java
here are some more differences between Serializable and Externalizable interfaces in Java:
1. In the case of Serializable, the default serialization process is used. while in the case of Externalizable custom Serialization process is used which is implemented by the application.
2. JVM gives call back to readExternel() and writeExternal() of java.io.Externalizalbe interface for restoring and writing objects into persistence.
3. Externalizable interface provides complete control of serialization process to application.
4. readExternal() and writeExternal() supersede any specific implementation of writeObject and readObject methods.
Though Externalizable provides complete control, it also presents challenges to serialize super type state and take care of default values in case of transient variable and static variables in Java. If used correctly Externalizable interface can improve the performance of the serialization process.
That’s all on the difference between Externalizable and Serializable interface in Java. This is always asked when Java interviews take turns towards Serialization after Multithreading questions and Collections Interview questions.
The only problem with Serialization is that not many programmers use it and that’s why it looks little difficult otherwise once you are familiar with Serialization process and rules of Serialization, interview questions can be more easily handled.
Some Interview Questions post:
Thanks for reading this article so far. Let me know if this question was asked to you on any Java interview? Also what is your favorite Java question related to Serialization?