Serializable vs Externalizable

N

Norris Watkins

I was reading about serialization.

1. I could not quite understand how Externalizable can be faster than a
Serializable object with readObject/writeObject implemented. After all both
of them assigns the responsibility to you.

2. Does it have something to do with the format in which the object is store
? ( Maybe like Serializable objects use Serialization protocol version 1,
while Externalizable uses the later protocol 2 ? )
 
J

John McGrath

1. I could not quite understand how Externalizable can be faster than a
Serializable object with readObject/writeObject implemented. After all
both of them assigns the responsibility to you.

When you use Serializable, the responsibility is not really assigned to
you, although you can take over *some* of the responsibility. If you do
not define a writeObject() method, you do not assume any responsibility -
the class will be automatically serialized. If you do define this method,
it will still take care of serializing the superclass. However, when you
use Externalizable, everything is up to you.
2. Does it have something to do with the format in which the object is
store ? ( Maybe like Serializable objects use Serialization protocol
version 1, while Externalizable uses the later protocol 2 ? )

When you use Externalizable, it only writes what you explicitly tell it to
write.
 
N

Norris Watkins

John McGrath said:
When you use Serializable, the responsibility is not really assigned to
you, although you can take over *some* of the responsibility. If you do
not define a writeObject() method, you do not assume any responsibility -
the class will be automatically serialized. If you do define this method,
it will still take care of serializing the superclass. However, when you
use Externalizable, everything is up to you.



Ha thanks a lot John:
I didnt know that even with writeObject() defined, it will write the
superclass "part" of the object.
I somehow thought that one has to call defaultWriteObject() inside
writeObject() for this to happen.
 
A

Alex Molochnikov

Norris Watkins said:
I didnt know that even with writeObject() defined, it will write the
superclass "part" of the object.
I somehow thought that one has to call defaultWriteObject() inside
writeObject() for this to happen.

Yes, one does have to call the defaultWriteObject() to serialize the
superclass, and the serializable ivars of the given class. However, keep in
mind that you can control what instance variables to serialize by using the
"transparent" attribute which excludes the ivar from the serialization
process. All in all, the Serializable and Externalizable can be tuned to the
same performance.

Alex Molochnikov
Gestalt Corporation
www.gestalt.com
 
A

Andrew McDonagh

Alex said:
Yes, one does have to call the defaultWriteObject() to serialize the
superclass, and the serializable ivars of the given class. However, keep in
mind that you can control what instance variables to serialize by using the
"transparent" attribute which excludes the ivar from the serialization
process. All in all, the Serializable and Externalizable can be tuned to the
same performance.

Alex Molochnikov
Gestalt Corporation
www.gestalt.com

I think you mean 'transient' as in...

private transient String aStringThatWOntBeSerialized = "wontBeSerialized";
 
A

Alex Molochnikov

Andrew McDonagh said:
Alex Molochnikov wrote:
I think you mean 'transient' as in...

private transient String aStringThatWOntBeSerialized = "wontBeSerialized";

Of course. That's what happens when one starts celebrating the New Year
early... :)
 
J

John McGrath

I somehow thought that one has to call defaultWriteObject() inside
writeObject() for this to happen.

Yes, but according to the Serialization specs, you *must* call it or the
writeFields() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top