After going through the article at http://geekexplains.blogspot.com/2008/06/diff-between-externalizable-and.html, i got to know Externalizable is better than Serializalable as it provides better control on code and also faster .So Externalizable should be preffered instead of Serializable provided class definition is not changed.But when i see in any project i find using Serializable interface only. can it be ignorance or Serializalable provides some other advantage which i am missing?
Serialization is a marker interface. Externalization contains two methods readExternal and writeExternal. 2. Implementation logic. The class which is implementing this interface gives the responsibility to JVM for serializing or persist java object.
Interface Externalizable The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state.
Differences between Externalizable vs Serializable Serializable is a marker interface i.e. does not contain any method. Externalizable interface contains two methods writeExternal() and readExternal() which implementing classes MUST override.
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. The byte stream created is platform independent.
The advantage of Serializable is it's incredibly easy to implement, and resilient to change (in most cases all you have to do is update the serialversionUID). Externalizable requires the programmer to actually do work, and do more work every time the contents of the class change. As the article you link to points out implementing Externalizable is also error-prone. So from the point of view of utilizing limited programmer time, often Serializable is a better choice.
The good thing about how Serializable and Externalizable are designed is that you can defer the decision to implement Externalizable until it becomes evident there's a performance problem, and you can selectively implement it only for those classes where there's a problem.
Serializable
is a marker interface that indicates that instances can be written to an output stream and read back. You don't have to write and code (you just have to ensure all fields are themselves Serializable
).
Externalizable
is a Serializable
that alos provides custom (de)serialization code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With