Simply put, the state of various objects stored in the memory can be read again. Although you can use your own methods to save object states, Java provides you with a mechanism better than your own to save the object state, that is, serialization.
2. Under what circumstances should serialization be performed?
A) when you want to save the objects in the memory to a file or database;
B) when you want to use a socket to transmit objects over the network;
C) when you want to transmit objects through RMI;
3. What happens when an object is serialized?
Before serialization, each object stored in heap has a corresponding state, that is, instance ariable, for example:
Foo myfoo = new Foo ();
Myfoo. setwidth (37 );
Myfoo. setheight (70 );
After the following code is serialized, the values of the width and height instance variables () in the myfoo object are saved to foo. in the SER file, you can read it from the file and create the original object in the heap. Of course, it is not only to save the instance variable value of the object, but also to save a small amount of information, such as the type of the class, to restore the original object.
Fileoutputstream FS = new fileoutputstream ("foo. Ser ");
Objectoutputstream OS = new objectoutputstream (FS );
OS. writeobject (myfoo );
4. serialize (save to a file)
A) make a fileoutputstream
Java code
Fileoutputstream FS = new fileoutputstream ("foo. Ser ");
B) Make a objectoutputstream
Java code
Objectoutputstream OS = new objectoutputstream (FS );
C) write the object
Java code
OS. writeobject (myobject1 );
OS. writeobject (myobject2 );
OS. writeobject (myobject3 );
D) Close the objectoutputstream
Java code
OS. Close ();
5. Examples
Java code
Import java. Io .*;
Public class box implements serializable
{
Private int width;
Private int height;
Public void setwidth (INT width ){
This. width = width;
}
Public void setheight (INT height ){
This. Height = height;
}
Public static void main (string [] ARGs ){
Box mybox = new box ();
Mybox. setwidth (50 );
Mybox. setheight (30 );
Try {
Fileoutputstream FS = new fileoutputstream ("foo. Ser ");
Objectoutputstream OS = new objectoutputstream (FS );
OS. writeobject (mybox );
OS. Close ();
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
6. Precautions
A) when a parent class is serialized, the Child class automatically serializes without explicitly implementing the serializable interface;
B) when the instance variable of an object references other objects, the referenced object is also serialized when the object is serialized;
C) Not all objects can be serialized. There are many reasons for this. For example:
1. security reasons: for example, an object has fields such as private and public. For an object to be transmitted, such as writing to a file, or performing RMI transmission, during serialization and transmission, the private and other fields of this object are not protected.
2. the reason for resource allocation, such as socket and thread classes, cannot be re-allocated if they can be serialized, transmitted, or saved. Moreover, there is no need to implement this.
Because session is used to transmit various values and objects, and objects cannot be directly transmitted over the network, they must be serialized.