Externalization

 Externalization

=====================================

  • In serialization every things take care by JVM and programmer have does not any control.
  • In serialization is always possible to save total object to the file and it is not possible to save part of the object, which may create performance problem. 
  • To overcome this problem we should go for externalization.
  • The main advantage of externalization over serialization is every thing take care by programmer and JVM does not have any control.
  • Based on our requirement we can save either total object or part of the object, which improve performance of the system.
  • To provide externalizability for any java object, compulsory the corresponding class implements Externalizable interface.
  • Externalizable interface define 2 methods.
 
 
  • Externalizable is the child interface of Serializable interface 
 
 

1. public void writeExternal(ObjectOutputStream oos) throws IOException

  • This method will be executed automatically at the time of serialization.  
  • Within this method we have to write code to save required variables to the file.
 

2. public void readExternal(ObjectInputStream ois) throws IOException 

  • This method will be executed automatically at the time of de-serialization.  
  • Within this method we have to write code to read required variable from the file and assign to current object.
  • But strictly speaking at the time of de-serialization JVM will create a separate new object by executing public no-argument constructor.
  • On that object JVM will call readExternal().
  • Hence every Externalizable implemented class should compulsory contain public no-argument constructor, otherwise we will get runtime exception: InvalidClassException

 Ex.


  • If the class implements Serializable interface then total object will be saved to the file. In this case
  • If the class implements Externalizable interface then only required variable will be saved to the file. In this case

Note: 

In serialization transient keyword will play role but in Externalization transient keyword would not play any role. Of course transient keyword not required in Externalization.
 

Important: Default constructor must have public modifier.

 

Difference between Serialization and Externalization

Serialization

  • In default Serialization every this take care by JVM, and programmers does not have any control.  
  • In this case it is always possible to save total object to the file.
  • Not possible to save part of the object.
  • Relatively performance is low.
  • Best choice for save total object to the file.
  • Marker interface so have no method.
  • Implemented class not required to contain public no argument constructor. 
  • transient keyword play a role.

Externalization

  • In Customized serialization every this take care by programmers and JVM does not have any control.
  • Based on our requirement we can save either total object or part of the object.
  • Possible to save part of the object.
  • Performance is high.
  • Best choice for save part of the object to file.
  • Not marker interface, have 2 methods writeExternal() and readExternal().
  • Implemented calss should compulsory contain public no argument constructor, otherwise get runtime exception: InvalidClassException.
  • No use of transient keyword.

 

Related Topic 


RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post