Wednesday, June 4, 2008

Diff between Externalizable and Serializable in Java


Difference between Externalizable and Serializable in Java

One obvious difference that Serializable is a marker interface and doesn't contain any methods whereas Externalizable interface contains two methods: writeExternal(ObjectOutput) and readExternal(ObjectInput). But, the main difference between the two is that Externalizable interface provides complete control to the class implementing the interface over the object serialization process whereas Serializable interface normally uses default implementation to handle the object serialization process.

While implementing Serializable, you are not forced to define any method as it's a marker interface. However, you can use the writeObject or readObject methods to handle the serilaization process of complex objects. But, while implementing Externalizable interface, you are bound to define the two methods: writeExternal and readExternal and all the object serialization process is solely handled by these two methods only.

In case of Serializable interface implementation, state of Superclasses are automatically taken care by the default implementation whereas in case of Externalizable interface the implementing class needs to handle everything on its own as there is no default implementation in this case.

Example Scenario: when to use what?

If everything is automatically taken care by implementing the Serializable interface, why would anyone like to implement the Externalizable interface and bother to define the two methods? Simply to have the complete control on the process. OKay... let's take a sample example to understand this. Suppose we have an object having hundreds of fields (non-transient) and we want only few fields to be stored on the persistent storage and not all. One solution would be to declare all other fields (except those which we want to serialize) as transient and the default Serialization process will automatically take care of that. But, what if those few fields are not fixed at design tiime instead they are conditionally decided at runtime. In such a situation, implementing Externalizable interface will probably be a better solution. Similarly, there may be scenarios where we simply don't want to maintain the state of the Superclasses (which are automatically maintained by the Serializable interface implementation).

Which has better performance - Externalizable or Serializale?

In most of the cases (or in all if implemented correctly), Externalizable would be more efficient than Serializable for the simple reason that in case of Externalizable the entire process of marshalling, un-marshalling, writing to the stream, and reading back from stream, etc. is under your control i.e., you got to write the code and you can of course choose the best way depending upon the situaton you are in. In case of Serializable, this all (or at least most of it) is done implicitly and the internal implementation being generic to support any possible case, can ofcourse not be the most efficient. The other reason for Serializable to be less efficient is that in this case several reflective calls are made internally to get the metadata of the class. Of course, you would not need any such call is needed in case Externalizable.

However, the efficiency comes at a price. You lose flexibility because as soon as your class definition changes, you would probably need to modify your Externaliable implementation as well. Additionally, since you got to write more code in case Externalizable, you increase the chances of adding more bugs in your application.

Another disadvantage of Externalizable is that you got to have the class to interpret the stream as the stream format is an opaque binary data. Normal Serialization adds field names and types (this why reflective calls are needed here) into the stream, so it's possible to re-construct the object even without the availability of the object's class. But, you need to write the object reconstruction code yourself as Java Serialization doesn't provide any such API at the moment. The point is that in case of Serialzable you can at least write your code as the stream is enriched with field names and types whereas in case Externalizable the stream contains just the data and hence you can't unless you use the class definition. As you can see Serializable not only makes many reflective calls, but also puts the name/type info into the stream and this would of course take some time making Serialzable slower than the corresponding Externalizable process where you got to stuff only the data into the stream.

Note: One of our visitors, Manish asked this question in response to the post on Externalizable interface. Thanks Manish for bringing this up and I hope this article will help you in understanding the differences between the two interfaces. Keep visiting/posting!

Liked the article? Subscribe to this blog for regular updates. Wanna follow it to tell the world that you enjoy GeekExplains? Please find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


22 comments:

Manish said...

Thanks Geek for answering my question. Like every other article I found this article also very well written. You make difficult points so easy to understand. Great!

Abbas said...

nice article.

Abbas said...

nice article.

Manoj said...

Excellent Explanation.
Thank You Very Much

Brian said...

Any chance you could comment on the relative performance of each interface? Is there overhead to the processing of the Externalizable methods that isn't obvious? My assumption is that since I write the methods, I am responsible for every aspect of performance - but this is not true of Serializable interface, even if I implement the readObject/writeObject methods.

Geek said...

Added answer to your query in the article above. Hope that helps!

itsmydatabase said...

Wowww nice explanation..Thanks

Neera said...

This is very good description b/w Externalizable and Serializable

Anonymous said...

I got a good idea on Serialization with this article...

Thank you...

PSReddy.

yhemanth said...

Nice article..

yhemanth said...

Nice article...

Deep s said...

Excellent

lakku said...

Nice article...
Very useful

lakku said...

Nice article

Suresh Chauhan said...

Thanks,
nic article

Suresh Chauhan said...

Thanks,
nice article

Javin said...

One of the major difference between Serializable and Extenalizable is Control over default Serialization process which gives you chance to optimize it. see here for some more difference between Serialization and Externalization

Anonymous said...

Very nice..

Anonymous said...

Superb blog you provided man! I was looking for this kind of internal stuff. I'm so happy know from your blog.

Jacob.

javainterviewquestions said...

Good Explanation with minute details on serializable and externalizable

www.java-interview-questions.com

Sandeep Bhandari said...

you have shared quite good points. Here is one more differentiator article for fellow developers to read on <a href="http://www.javaexperience.com/java-externalizable-vs-serializable-interfaces/'>serializable vs externalizable</a>

Anonymous said...

Hi its a great explanation... Prateek