Author: scruffybear
Release time: 1/11/2006
Company: watchdata
If any reprint is available, please indicate the source and maintain the integrity of the Article. Thank you!
Java serialization provides a great mechanism for storing the object State. To put it bluntly, serialization stores the object state on the hard disk, you can read and use it as needed. However, when storing the object status, we sometimes need to not store specific object data during serialization. At this time, the transient keyword will be used. You can use the transient keyword to define a specific data domain of the class. For the underlying Java virtual machine, the transient type variable is not a permanent state of the class.
The following is the experiment code for the transient Keyword:
Import java. util .*;
Import java. Io .*;
Public class logginginfo implements java. Io. serializable
{
Private date loggingdate = new date ();
Private string uid;
Private transient string PWD;
Logginginfo (string user, string password)
{
Uid = user;
Pwd = password;
}
Public String tostring ()
{
String Password = NULL;
If (Pwd = NULL)
{
Password = "not set ";
}
Else
{
Password = PWD;
}
Return "Logon info:/N" + "User:" + uid +
"/N logging Date:" + loggingdate. tostring () +
"/N password:" + password;
}
Public static void main (string ARGs []) {
Logginginfo loginfo = new logginginfo ("Mike", "mechanic ");
System. Out. println (loginfo. tostring ());
Try
{
Objectoutputstream o = new objectoutputstream (
New fileoutputstream ("loginfo. Out "));
O. writeobject (loginfo );
O. Close ();
}
Catch (exception e) {// deal with exception}
System. Out. println ("Hello world! ");
}
Try
{
Objectinputstream in = new objectinputstream (
New fileinputstream ("loginfo. Out "));
Logginginfo loginfo1 = (logginginfo) in. readobject ();
System. Out. println (loginfo1.tostring ());
}
Catch (exception e ){
// Deal with exception
}
}
}
The loginfo. Out file is generated for serialization to store Class Object Data. After being stored, the file is read. The read content is as follows.
The output result is as follows:
Logon info:
User: Mike
Logging Date: Mon Oct 30 21:39:58 CST 2006
Password: Mechanics
Logon info:
User: Mike
Logging Date: Mon Oct 30 21:39:58 CST 2006
Password: Not Set
We can see that the Read Password is mechanic, but the Read Password is not set, because it is not stored on the hard disk during serialization, because PWD is defined as transient.
The transient keyword also produces side effects. See the following code:
Public class guestlogginginfo implements java. Io. serializable
{
Private date loggingdate = new date ();
Private string uid;
Private transient string PWD;
Guestlogginginfo ()
{
Uid = "guest ";
Pwd = "guest ";
}
Public String tostring ()
{
// Same as abve
}
}
The read PWD or not set, that is, the default initialization does not work.
In addition, record the Writing Method of Java code throwing exceptions.
For example, I want to throw a null pointer exception:
If (comparebuffer = NULL ){
Nullpointerexception E = new nullpointerexception ("The comparebuffer is null! ");
Throw E;
}
In fact, nullpointerexception is inherited from runtimeexception, while runtimeexception is inherited from exception.
Over!