Difference between Serialization & Externalization?

Difference between Serialization & Externalization?

Difference between Serialization & Externalization?

Difference between Serialization & Externalization?
  • In serialization, it is always possible to total object to the file and not possible to save part of the object. Which may create performs problem.
  • To overcome this problem we should go for Externalization.
  •  The main advantage of Externalization over Serialization is everything takes care by programmer and JVM doesn’t have any control.
  • There might be times when you have special requirements for the serialization of an object. For example, you may have some security-sensitive parts of the object, like passwords, which you do not want to keep and transfer somewhere. Or, it may be worthless to save a particular object referenced from the main object because its value will become worthless after restoring.
  • You can control the process of serialization by implementing the Externalizable interface instead of Serializable. This interface extends the original Serializable interface and adds writeExternal() and readExternal(). These two methods will automatically be called in your object’s serialization and deserialization, allowing you to control the whole process.
  • Based on over requirement we can save either total object or part of the object. Which improves performs of the system.
  • To provide the Externalizable ability for any Java object compulsory the corresponding class should implement the Externalizable interface.
  • The Externalizable interface defines two methods.
    • writeExternal Public void writeExternal(ObjectOutput os) throws IOException
      1. This method will be executed automatically at the time of serialization.
      2. Within this method, we have to write code to save a required variable to the file.
    • readExternal Public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException
      1. This method will be executed automatically at the time of deserialization.
      2. Within this method, we have to write code to read a required variable from the file and assign to the current object.
      3. But strictly speaking at the time of deserialization JVM will create a separate new object by executing public no-argument constructor.
      4. On that object, JVM will call readExternal() method.
  • If the class implements Serializable the total object will be saved to the file.
  • If the class implements Externalizable then only required variable will be saved to the file.
  • Note:- In serialization transient keyword will play a role but in Externalization transient keyword won’t play any role of course transient keyword not required in externalization.

Leave a Reply

Your email address will not be published. Required fields are marked *

*