File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Difference between serialization of Vector and ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between serialization of Vector and ArrayList" Watch "Difference between serialization of Vector and ArrayList" New topic
Author

Difference between serialization of Vector and ArrayList

Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Hi all

Could anyone please throw some light on why the serialization of Vector and ArrayList is handled differently?
The object element array used internally in ArrayList is marked transient but not in Vector. Vector uses default Serialization approach accept making the writeObject method synchronized while ArryList manually writes and reads every element.

Regards.

Girish K Gupta
SCJP 1.5 - 87%, SCWCD 1.5 - 92%
India
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

I've looked at the source code of the ArrayList.writeObject() method.

I think the reason why it has its own writeObject() method rather than relying on the default (built-in) serialization is because it needs to check for modifications in the method, throwing a ConcurrentModificationException if the ArrayList was modified by another thread while it was busy writing its contents to a stream.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Thanks for your reply Jesper. I understand the point that for ArrayList ConcurrentModificationException has to be thrown in case of modification while writing the object.
In ArrayList writeObject method looks like:

While for Vector it is:

What I want to understand is why ArrayList separately writes each element which doesn't happen for Vector.
Could we not have the following code for ArrayList:
>
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

That won't work for ArrayList, because its internal Object array is declared transient, and defaultwriteObject() doesn't write transient fields to the stream.
Writing the array's size and actual contents instead of the entire array itself is slightly more efficiƫnt, because the array would add it's own overhead to the serialization process.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Thanks Jelle. I didn't know about the following:
Writing the array's size and actual contents instead of the entire array itself is slightly more efficiƫnt, because the array would add it's own overhead to the serialization process


Do you mind sharing the source of this information? Would do some study
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Why not just give it a try?
Initialize an Object array, and stuff one String into it. Serialize the int returned by the array's length property, using an ObjectOutpuStream#write, followed by the String instance using ObjectOutpuStream#writeObject() to a file. Then serialize the entire array, using ObjectOutpuStream#writeObject() to another file. You should be able to get a pretty good impression of the difference between these two files using any HEX editor.
Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Yes Jelle I noticed the difference. Tried serializing object of an ArrayList and a Vector filled with same objects.
In my case serialized ArrayList object took 230 bytes while serialized Vector object took 335 bytes of memory.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Ok, but that doesn't really clearly represent the difference between serializing an entire array versus serializing the size of the array an all its elements in order.
 
 
subject: Difference between serialization of Vector and ArrayList