5

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?

MarianP
  • 2,719
  • 18
  • 17
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • 1
    You might find this interesting of you are concerned about speed. http://vanillajava.blogspot.com/2011/08/avoiding-java-serialization-to-increase.html – Peter Lawrey Aug 28 '11 at 15:07
  • I've charted a benchmark comparing various serialization options here: http://java-is-the-new-c.blogspot.de/2013/10/still-using-externalizable-to-get.html – R.Moeller Oct 11 '13 at 21:23
  • The only case when `Serializable` saves code over `Externalizable` is when all the non-`transient` instance fields of a class are a good logical representation of it. If not, then accepting the default serialization can cost you a lot more code and maintenance time over the long haul than if you had used `Externalizable` to design a good serial form in the first place. – scottb Jan 27 '15 at 23:28

3 Answers3

6

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.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Agreed that Serializable is it's incredibly easy to implement.But its slower than Externalizable too. As i said if class definition does not change we should go with Externalizable as per my thoghts.Just because its easy to implement at the cost of performance is not the way we sshould look for. – M Sach Aug 28 '11 at 14:06
  • 3
    @Mohit: there is more to programming than performance. if something is not a bottleneck, then optimizing it is a waste of time. Say you have a clustered webapp with serializable domain objects that have to go in the session, the nodes are linked with a high-speed network, micro-optimizing their serialization may not pay off. – Nathan Hughes Aug 28 '11 at 14:07
4

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

I got to know Externalizable is better than Serializalable

That link doesn't say that. There is no 'better' in this situation, it is horses for courses. If you are prepared to write lots more code, continually over the lifetime of the project, Externalizable may be 'better' in some senses, e.g. space and time costs. If the cost of code is a concern, Serializable is a great deal 'better'. And these aren't the only alternatives.

user207421
  • 305,947
  • 44
  • 307
  • 483