Java serializable experience
Serializable: serializable.
Serialization)
Serialization Definition
1. What is serialization?
The lifetime of an object is usually terminated with the termination of the program that generates the object. Sometimes, you may need to save the object state and restore the object as needed. The ability to record the state of an object for future regeneration is called the object persistence ). An object Records itself by writing a value describing its own State. This process is called serialization ).
2. serialization objective
The purpose of serialization is to provide a set of features for the Java Runtime Environment. Its main task is to write the value of the object instance variable. Source: www.examda.com
Serialization Method
In the java. Io package, the interface serializable is used as a tool for serializing objects. Only objects of serializable classes can be serialized.
1. Define a serializable object
Public class student implements serializable {
Private Static final long serialversionuid = 1l;
Int ID;
String name;
Int age;
String Department; // Department
Public student (int id, string name, int age, string Department ){
This. ID = ID;
This. Name = Name;
This. Age = age;
This. Department = Department;
}
}
During serialization, a version number called serialversionuid is used to associate with each serializable class, in the deserialization process, this serial number is used to verify whether the sender and receiver of the serialized object have loaded classes compatible with serialization for this object. If the serialversionuid of the class loaded by the receiver is different from the version of the class of the corresponding sender, deserialization will lead to invalidclassexception. The serializable class can explicitly declare its own serialversionuid by declaring a field named "serialversionuid" (which must be a static and final long field:
Private Static final long serialversionuid = 1l; if the serialversionuid is not explicitly declared for the serializable class, the default serialversionuid value of the class will be calculated based on all aspects of the class during serialization, as described in "Java (TM) Object serialization specification. However, it is strongly recommended that all serializable classes explicitly declare the serialversionuid value. The reason for calculation is that the default serialversionuid is highly sensitive to the class details, which may vary according to compiler implementations, in this way, unexpected invalidclassexception may occur during deserialization. Therefore, to ensure the consistency of serialversionuid values across different Java compilers, the serialization class must declare a clear serialversionuid value. We strongly recommend that you use the private modifier to display the Declaration serialversionuid (if possible), because this declaration is only applicable to the immediate declaration class -- serialversionuid field as an inherited member.
2. Construct the input/output stream of the object
To serialize an object, it must be associated with a certain object input/output stream. The object state is saved through the object output stream, and the object state is restored through the object input stream.
In the java. Io package, objectinputstream and objectoutputstream are provided to extend the data stream function to read/write objects. In objectinputstream, The readobject () method can be used to directly read an object. In objectoutputstream, The writeobject () method can be used to directly Save the object to the output stream. Source: www.examda.com
Public static void main (string [] ARGs ){
System. Out. println ("enters the main method... ");
Student Stu = new student (981036, "Liu sui", 18, "CSD ");
// Save the object status
Try {
Fileoutputstream fo = new fileoutputstream ("data. Ser ");
Objectoutputstream so = new objectoutputstream (FO );
So. writeobject (Stu );
So. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
// Restore the object state
Try {
Fileinputstream Fi = new fileinputstream ("data. Ser ");
Objectinputstream Si = new objectinputstream (FI );
Try {
Stu = (student) Si. readobject ();
System. Out. println ("student ID =" + Stu. ID );
System. Out. println ("Student name =" + Stu. Name );
System. Out. println ("student age =" + Stu. Age );
System. Out. println ("Student Department =" + Stu. Department );
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}
Si. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
In this example, we first define a class student, implement the serializable interface, and then save the student object to the file data. Ser through the writeobject () method of the object output stream. Then, read the stored student object from the file data. Ser using the readobject () method of the object input stream.
Precautions for serialization
1. serialize the elements that can be saved
Only non-static member variables of the object can be saved, and no member methods and static member variables can be saved in a serialized manner. Only the value of the variable is saved. Any modifier of the variable, cannot be saved.
2. Transient (transient, instantaneous) KEYWORDS
For some types of objects, their statuses are instantaneous. Such objects cannot save their statuses, such as a thread object or a fileinputstream object. For these fields, we must use the transient keyword to indicate
3. Custom serialization
The default serialization mechanism is used. objects are serialized first, information about class data and class fields is written, and then their values are written in the ascending order of names. If you want to explicitly control the write sequence and types of these values, you must define your own data stream reading method. The writeobject () and readobject () methods are rewritten in the class definition.
For example, you can add the rewritten writeobject () and readobject () methods in the example to customize the serialization of the student class.
Private void writeobject (objectoutputstream out) throws ioexception
{
Out. writeint (ID );
Out. writeint (AGE );
Out. writeutf (name );
Out. writeutf (Department );
}
Private void readobject (objectinputstream in) throws ioexception
{
Id = in. readint ();
Age = in. readint ();
Name = in. readutf ();
Department = in. readutf ();
}
Public class student implements serializable {
} To put it bluntly, it is:
The object that implements the serializable interface is converted to a byte sequence, which can be fully stored for future re-generation of the original object. That is, you can save the object to the byte stream and restore it! Therefore, if your object is not serialized, how can it be transmitted over the network? To transfer the object over the network, You have to convert it into a byte stream. Therefore, in distributed applications, You have to implement serialization, if you do not need distributed applications, there is no need to implement serialization.