• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serializable vs Externalizable

 
Prashant Sehgal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know there've been plenty of discussion around this, but I could not find a satisfatory answer as to why Externalizable is really required.

I can achieve an extremely high level of customization by manually overriding readObject and writeObject methods of Serializable just as I can by overriding readExternal and writeExternal of Externaliable.

The sun documentation page on Externalizable says that...
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


...but can't the same level of customization be gained by overriding Serializable?
 
Prashant Sehgal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So no replies means dumb question or dumb audience ...?
 
Pradheep Thiruvengadam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither audience nor the question is dumb.. some times it also means that ppl. are busy Let me try to explain ..as far as I know.

To be frank, If you can understand the API doc of externalizable from JDK documentation, you are not a normal person .

Coming to the answer,

You must have noticed that Externalizable extends Serializable. Hence you can't put "VS" in between them. Externalizable is an enhancement over Serializable.

OK this is the theory behind Externalizable..

JVM has a mechanism to write or read an object from the I/O streams using its own representation (bytecode). This is done through readObject() and writeObject() APIs.

Assume if you want store the object in your own way, you must do with Externalizable.

It will look bit wiered, but thats how it is...

If you have Thinking in Java (Bruce Eckel), please refer it. The same example is available in this URL
Java2S

Hope it helps.
 
Pradheep Thiruvengadam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the applications...

Suppose if you want to serialize an object which can be readable for an C++ program you can do it with externalizable. Assuming both entities understand the binary format.
 
Prashant Sehgal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still dont get it - cant the same thing be achieved by overriding readObject() and writeObject() of Serializable??
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

You can serialize only the required fields if you use Externalizable. But you must follow the order while retrieving back. This is the disadvantage of using Externalizable.

But in Serializable, all fields except fields which are marked static or transient are getting serialized. Here you dont need to care about the order.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!