File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How is serialization handled with transient variables defined in Collections ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How is serialization handled with transient variables defined in Collections ?" Watch "How is serialization handled with transient variables defined in Collections ?" New topic
Author

How is serialization handled with transient variables defined in Collections ?

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

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

Regards
KumarRaja

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2374
    
  28

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

Joined: Mar 18, 2010
Posts: 518
    
    2


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 ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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

Joined: Mar 18, 2010
Posts: 518
    
    2

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How is serialization handled with transient variables defined in Collections ?