File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between serialization of Vector and ArrayList

 
Girish K Gupta
Greenhorn
Posts: 19
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Girish K Gupta
Greenhorn
Posts: 19
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Girish K Gupta
Greenhorn
Posts: 19
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic