• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between serialization of Vector and ArrayList

 
Greenhorn
Posts: 19
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
>
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic