SERIALIZABLE VS EXTERNALIZABLE

Serializable

  • Class should implement Serializable Marker Interface
  • JVM is responsible for serializing the class and its super classes provided they also implement Serializable interface
  • Transient and static variables are not serialized
  • Performance cannot be controlled much.
  • Based on recursive algorithm.  Apart from fields, even the objects refereed in the class is handled for serialization recursively provided the super classes also implement Serializable interface
  • Does not call constructor of the object during serialization/deserialization
  • Uses Reflection for recover object.
  • This causes lot of metadata information of the class to be added to the stream triggering performance and bandwidth issue.
  • Reads required information about the object from ObjectInputStream and Reflection
  • serialVersionUID should be generated explicitly. If not jvm will generate one using all fields, methods of the class which in turn takes not only long time but if anything in the class changes this ID will also change causing the object unable to recover .
  • Object is saved using ObjectOutputStream
  • Object is read using ObjectInputStream

Externalizable

  • Class should implement Externizable – contains 2 methods
    • writeExternal()
    • readExternal()
  • Responsiblity of the developer to handle the serialization and deserialization using the above 2 methods
  • Can control performance based on the design
  • public non-org constructor is needed
  • Fast and consumes less memory
  • Object is saved using the logic added in writeExternal() method.
  • Object is read using the logic added in readExternal() method.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Cheat Sheet To JAVA Latest Technology

%d bloggers like this: