The usage of transient in the source code, and the usage of the source code transient

Source: Internet
Author: User

The usage of transient in the source code, and the usage of the source code transient

Java serialization provides a mechanism for persistence object instances. When a persistent object is stored, there may be a special object data member. We do not want to use the serialization mechanism to save it. To disable serialization on a domain of a specific object, you can add the keyword transient before this domain. transient is the keyword of the Java language to indicate that a domain is not a part of the object's serialization. When an object is serialized, the value of the transient variable is not included in the serialized representation. However, non-transient variables are included.

The serialization code is as follows:

 1 public class LoggingInfo implements java.io.Serializable {    2     private Date loggingDate = new Date();    3     private String uid;    4     private transient String pwd;    5        6     LoggingInfo(String user, String password) {    7         uid = user;    8         pwd = password;    9     }   10     public String toString() {   11         String password=null;   12         if(pwd == null) {   13             password = "NOT SET";   14         }   15         else {   16             password = pwd;   17         }   18         return "logon info: \n   " + "user: " + uid +   19             "\n   logging date : " + loggingDate.toString() +   20             "\n   password: " + password;21     }22 }

Now we create another instance of this class, serialize it, and write this serialized object as a disk.

 1 LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS"); 2 System.out.println(logInfo.toString()); 3 try { 4    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("logInfo.out")); 5    o.writeObject(logInfo); 6    o.close(); 7 }    8 catch(Exception e) {//deal with exception} 9 To read the object back, we can write10 try {11    ObjectInputStream in =new ObjectInputStream(new FileInputStream("logInfo.out"));12    LoggingInfo logInfo = (LoggingInfo)in.readObject();13    System.out.println(logInfo.toString());14 }15 catch(Exception e) {//deal with exception}

When running this code, we will notice that the read back (read -- back (de-serializing) object from the disk prints the password as "not set ". This is the expected result when we define the pwd domain as transient. If you remove the modifier transient before the password, the password will not output any content again.

 

So when will this keyword be used? What are the considerations? I think there are two possible considerations:

1. The number of values stored in the table in HashMap is smaller than the size of the array (the reason for Array resizing), which is more obvious when more elements are available. If default serialization is used, the locations without elements will also be stored, resulting in a lot of unnecessary waste.

2. for HashMap (and the underlying implementation uses HashMap's HashSet), because different virtual machines may have different Code values for the same hashCode, if you use the default serialization, after deserialization, the position of the element is consistent with that of the previous one. However, because the hashCode value is different, the element subscript returned by the positioning function indexOf () will be different, this is not the expected result.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.