Serialization: The process of converting a data structure or object into a binary string.
Deserialization: The process of converting a binary string generated during serialization into a data structure or object.
The use of transient
In the actual development process, we often encounter such a problem, some properties of this class need to serialize, and other attributes do not need to be serialized, for example, if a user has some sensitive information
(such as password, bank card number, etc.), in order to be safe, do not want to be transmitted in the network operation (mainly involves the serialization operation, the local serialization cache is also applicable), the variables corresponding to the information can be
Plus transient keyword
The life cycle of a field using transient is stored only in the caller's memory and is not persisted to disk.
Core Code :
1 Private Static Final LongSerialversionuid = 8294180014912103005L; 2 3 PrivateString username;4 Private transientString passwd;5 6User User =NewUser ();7User.setusername ("Tom");8USER.SETPASSWD ("123");9 Ten OneObjectOutputStream OS =NewObjectOutputStream ( A NewFileOutputStream ("C:/user.txt")); -Os.writeobject (user);//write the user object into a file - Os.flush (); the os.close (); - -ObjectInputStream is =NewObjectInputStream (NewFileInputStream ( -"C:/user.txt")); +user = (user) is.readobject ();//reading user data from a stream -Is.close ();
Attention:
1) Once the variable is transient modified, the variable will no longer be part of the object persistence, and the variable content cannot be accessed after serialization.
2) The Transient keyword can only modify variables, not methods and classes. Note that local variables cannot be modified by the transient keyword. If the variable is a user-defined class variable, the class
The serializable interface needs to be implemented.
3) Variables modified by the Transient keyword can no longer be serialized, and a static variable cannot be serialized, whether or not it is transient modified.
3rd, some people may be confused, because the program operation result is found to be unchanged after adding the static keyword to the username field in the user class
The truth is this: the 3rd is true (a static variable cannot be serialized regardless of whether it is transient), and the value of the static variable username in the deserialized class is the current
The value of the corresponding static variable in the JVM, which is not deserialized in the JVM
1 Private Static Final LongSerialversionuid = 8294180014912103005L; 2 3 Public StaticString username;4 Private transientString passwd;5 6User User =NewUser ();7User.setusername ("Tom");8USER.SETPASSWD ("123");9 TenObjectOutputStream OS =NewObjectOutputStream ( One NewFileOutputStream ("C:/user.txt")); AOs.writeobject (user);//write the user object into a file - Os.flush (); - os.close (); the - //change the value of username before deserializing -User.username = "Jack"; - +ObjectInputStream is =NewObjectInputStream (NewFileInputStream ( -"C:/user.txt")); +user = (user) is.readobject ();//reading user data from a stream AIs.close ();
Java serialization and deserialization