Understanding transient variables in Java and how they are practically used in HashMap—reference

What is the significance of the transient keyword in ? If you know the answer,good! you are a person who uses this a lot or a person who has read this very recently. If this seems like a word from a half remembered dream,well don’t worry you have company. I was and am will be confused if you asked me about this in an hour. It is one of those things that I learnt but never had to use it – mainly because I never worked on code that required me to worry about how my objects were serialized. I could delegate that to the libraries.

This post is an attempt to explain what transient is (which most people should know) and an example of how it is used,using the most abused class of my daily work – the. class.

The transient keyword in Java is used on and is used to indicate to the runtime that this variable should not be considered while serializing the object is being written to a. To put it in simpler words,if an object is being written to a byte stream then all transient variables will be ignored.

Lets say you have a flag that indicates whether the object was modified or not – this is typical in libraries that want to track whether a bean was modified after you retrieved it from the database. The value of the flag that indicates whether the object was modified or not is significant only until the change to the object is saved somewhere and the object is now again clean. So such a flag,lets call it the dirty indicator,is a field that gets reset once it is saved.

Based on the class name,the on the other end instantiates an object and sets all the values on the respective fields. Since you are sending all the field names and values,it will take a good amount of memory to hold this. If the size of the serialized byte array is larger then it will take more time to send this over the socket and it will take more time to reconstruct it. It will also consume more bandwidth.

There are applications where spending time sending large blocks of data is expensive – so you want to minimize the amount of space occupied by serialized objects and also the time taken to and de-serialize objects. You will write custom code to serialize your objects by writing writeObject(ObjectOutputStream) and readObject(ObjectInputStream) methods.

If you look at the source code of (),you will see that the data that you put in is stored in a array of Entry objects. This array is a transient variable called table. There is a table size which is transient too,and there are a bunch of other non-transient and transient variables.

Where hash is the of Object. ().

For this reason,HashMap implements the writeObject and readObject methods. These still let the non transient fields to get serialized as they would normally do,but they wrote the at the end of the byte array one after the other. When reading back,they let the non transient variables to be handled by Java default de-serialization logic and then read the key value pairs one by one.

These are all the contents of understanding transient variables in Java and how they are practically used in HashMap --- reference collected and sorted out for you by programming house. I hope this article can help you solve the program development problems encountered by understanding transient variables in Java and how they are practically used in HashMap --- reference.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>