Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Externalizable or Serializable?

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?

like image 507
M Sach Avatar asked Aug 28 '11 13:08

M Sach


People also ask

What is externalization and serialization?

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.

What is Externalizable in Java?

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.

Is Externalizable and Serializable a marker interface?

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.

What is the difference between serialization and deserialization?

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.


2 Answers

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.

like image 51
Nathan Hughes Avatar answered Oct 12 '22 07:10

Nathan Hughes


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.

like image 26
Bohemian Avatar answered Oct 12 '22 05:10

Bohemian