Before discussing transient, it is necessary to understand the meaning of serialization in Java;
The serialization of objects in Java refers to the conversion of an object into a sequence of bytes that contains the data and information of the object, a serialized object can be written to a database or file, or it can be used for network transmission. Generally, when we use cache caches (where there is not enough memory space to store locally on the hard disk) or remotely call RPC (network transport), we often need to have our entity classes implement the Serializable interface to make it serializable.
Of course, the final goal of serialization is to deserialize, revert to the original Java object, or else after serialization, so the serialized sequence of bytes can be restored to the Java object, the process is deserialization.
The role of the transient keyword in Java, simply, is to let some of the modified member property variables not be serialized, which seems to be well understood, is not serialized, then, in what case, some fields of an object do not need to be serialized? You might consider using the keyword transient adornment if:
1, the field values in the class can be deduced according to other fields, such as a rectangle class has three properties: length, width, area (example, generally do not design), then at the time of serialization, the area of the property is not necessary to be serialized;
2, other, see the specific business needs bar, which fields do not want to be serialized;
PS, remember before looking at HashMap source code, found that there is a field is decorated with transient, I think there is a reason, really do not need to serialize this modcount field, because there is no meaning, Modcount is mainly used to determine whether HashMap is modified (like put, remove operation, Modcount will be self-increment), for this variable, can be any value at the beginning, 0 of course it can be (new out, deserialized, Or clone clones come out with 0), it is not necessary to persist its value.
/** * The number of times this HashMap have been structurally modified * Structural Modifications is those that C Hange The number of mappings in * the HASHMAP or otherwise modify its internal structure (e.g., * rehash). This field was used to make iterators on collection-views of * The HashMap fail-fast. (see concurrentmodificationexception). */ transient int Modcount;
Finally, why not be serialized, mainly to save storage space, the other feeling is not good, there may be disadvantages (some fields may need recalculation, initialization, etc.), in general, the advantages outweigh the disadvantages.
Just an example, please use the actual situation as follows:
Packagetmp;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importjava.io.Serializable;classRectangleImplementsserializable{/** * */ Private Static Final LongSerialversionuid = 1710022455003682613L; PrivateInteger width; PrivateInteger height; Private transientInteger area; PublicRectangle (integer width, integer height) { This. width =width; This. Height =height; This. Area = width *height; } Public voidSetarea () { This. Area = This. Width * This. Height; } @Override PublicString toString () {StringBuffer sb=NewStringBuffer (40); Sb.append ("Width:"); Sb.append ( This. width); Sb.append ("\nheight:"); Sb.append ( This. Height); Sb.append ("\narea:"); Sb.append ( This. area); returnsb.tostring (); }} Public classtransientexample{ Public Static voidMain (String args[])throwsException {Rectangle Rectangle=NewRectangle (3,4); System.out.println ("1. Original object \ n" +rectangle); ObjectOutputStream o=NewObjectOutputStream (NewFileOutputStream ("Rectangle")); //writing an object to a streamo.writeobject (Rectangle); O.close (); //reading an object from a streamObjectInputStream in =NewObjectInputStream (NewFileInputStream ("Rectangle")); Rectangle Rectangle1=(Rectangle) in.readobject (); System.out.println ("2. Deserialized object \ n" +rectangle1); Rectangle1.setarea (); System.out.println ("3. Revert to original object \ n" +rectangle1); In.close (); }}
Original address: http://www.cnblogs.com/chenpi/p/6185773.html
Keyword transient in Java