• 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

How is serialization handled with transient variables defined in Collections ?

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

When I was looking at the code of HashSet, I noticed that it implements its functionality using a HashMap and interestingly this HashMap is a transient, which by definition need not be presisted when serialized. As we know HashSet is a Serializable object.

So writeObject() is called, how is it HashMap which is transient is actually persisted and later retrieved when readObject is called.

I guess the same thing with ArrayList and I believe almost all Collections implement their functionality using transient objects.

So why transient at its first place, when we know that those implementations are serializable.

another naive question is, I see readObject and writeObject methods of HashSet are private to HashSet. But I don't see any reference of these methods being called with in the HashSet class. So, how are these methods being called.

Thanks
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the implementation of readObject and writeObject of hashMap. Even though data members are transient, it is reading/writing out all the data members that carry the relevant data. They are not really transient

The reason why they did this is the entry table of the HashMap is sparse, so they made the table transient and overrode the read/Write method to put the data in a non-sparse manner.
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Look at the implementation of readObject and writeObject of hashMap. Even though data members are transient, it is reading/writing out all the data members that carry the relevant data. They are not really transient



That is exactly my point. If they are not transient, why to use the keyword.


The reason why they did this is the entry table of the HashMap is sparse, so they made the table transient and overrode the read/Write method to put the data in a non-sparse manner.



What do you meant by sparse table here? and how would it become non-sparse by making them transient. I mean, what relation does transient have with being sparse or not sparse.


Also, as I questioned earlier, I see the readObject and writeObjects as private. So how are these methods actually being called. I mean I don't see any reference elsewhere in these classes. or Am I missing anything here ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Raja wrote:


Look at the implementation of readObject and writeObject of hashMap. Even though data members are transient, it is reading/writing out all the data members that carry the relevant data. They are not really transient



That is exactly my point. If they are not transient, why to use the keyword.



It is marked as transient, so that the default serialization mechanism -- which in this case, are the defaultWriteObject() and defaultReadObject() calls -- do not serialize that variable. This allows the writeObject() and readObject() methods to write that variable in a very specific way.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Raja wrote:


The reason why they did this is the entry table of the HashMap is sparse, so they made the table transient and overrode the read/Write method to put the data in a non-sparse manner.



What do you meant by sparse table here? and how would it become non-sparse by making them transient. I mean, what relation does transient have with being sparse or not sparse.



Basically, there is a ton of stuff that doesn't need to be serialized -- for example, the buckets, and the internal linked lists, etc. The data structure is sparse meaning that serializing it is not very efficient -- only a small portion is the data when compared to the rest of the data structures. The writeObject() and readObject() methods only send (and retrieve) the data. The rest of the data structure is kinda recreated when it is deserialized.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Raja wrote:Also, as I questioned earlier, I see the readObject and writeObjects as private. So how are these methods actually being called. I mean I don't see any reference elsewhere in these classes. or Am I missing anything here ?




This is done internally by the JVM. During serialization (or deserialization), if a writeObject() (or readObject()) method is found, it is used to serialialize (or deserialize) that class portion of the object. These methods aren't really public, because it is specific to the class itself, and not the whole object (sort of like how constructors are only responsible for a portion of the instance).

Basically, the rules of Java seems to be thrown away when it comes to serialization. I think that it may be good to understand how it works, and understand that it is different from how other things work in Java.

Henry
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jayesh and Henry for explaining these internal concepts of serialization and sparse table structure.

I did try to debug a sample code dealing with serialization/deserialization with an intent of seeing how those private methods are being invoked and as you mentioned, I see a lot of complex code involved and I guess those private methods are called by reflection framework. I just wanted to check, if my finding about reflection in this scenario is correct or not.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic