1. What is serialization
Java is an object-oriented programming language, and sometimes it is necessary to save the object, and it can be restored smoothly the next time it is used. Because this requirement is common, the Java API supports this by adding the relevant program code to the standard class library and calling the save and restore Process "object serialization."
The Java SE7 document will be described in detail with the relevant content of the object serialization, which is referred to as:
Java Object Serialization Specification Java serialization specification, URL:
Http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html
2. Why serialization is called
Personal guess:
Because the process of saving the object is to save the object as a series of byte stream, and the meaning of English serialization "serialization", so the serialized image of the expression of the process.
3. Serialize and save the content
Object is an instance of Class (instance). A class contains two parts of a variable (field) and a function (method). The different objects of the same class are just variables, so the Java API serialization process only saves the variable parts of the object. Similarly, because static variables (the Statics field) are shared by individual objects of the same class, they are not saved during serialization.
Because the object needs to be created dynamically in the program when the object is restored, the program also needs to know the class definition of the object, so if the object is serialized by one program and saved by another program, the class file also needs to be routed to the program. This requires extending the functionality of the Java API serialization to customize it. Java's remote method invocation (invocation, RMI) functionality is based on the Java API serialization and is extended.
3, the use of serialization
There are three main uses of serialization:
Object persistence refers to prolonging the existence time of an object. Normally, when the program ends, the object in the program no longer exists.
If you save the object to a file by serializing it, you can lengthen the object's presence time and restore the object the next time the program runs.
Serialization saves objects in a file, which is one way to implement object persistence. There are many ways to persist, such as the Hibernate framework, which provides a complete set of object persistence scenarios.
By serializing, the object is saved in memory and can then be used to get a copy of multiple objects.
By serializing, the object is converted to a byte stream and can be sent over the network to another Java program.
4, what is stream (stream)
Java is an object-oriented programming language, and objects are abstract representations of real entities. So the Java API stream is an abstraction of a series of data, as well as some operations, write, read, and so on. So a real entity can be represented as a stream as long as it contains data and reads and writes to the data. The OutputStream class and the InputStream class are 2 abstract classes, each corresponding to the output, the input stream, and all other stream objects, all of which are subclasses.
For example, the file is essentially a series of data stored in the storage device, in the Java API is abstracted into the FileOutputStream class and the FileInputStream class, the read and write of the file can be realized by reading and writing the corresponding stream.
For example, console commands and results input and output, keyboard input is a string of data, the output of the program is a string of data, so in the Java API is also abstracted as a stream object. The console input is represented by the System.in object, and System.in is an object of type InputStream. The console output is represented by the System.out object, and System.out is an object of type PrintStream.
Because both the file and console input and output are related to the operating system, the file stream and console stream objects are ultimately created by the Java Virtual machine.
Bytearrayoutputstream, Bytearrayinputstream, is a stream object that is completely independent of the Java Virtual machine, which is entirely an abstraction of an byte[] array. Because the byte[] array is also a series of data, the byte[] array supports read and write functions, so it can be abstracted as a stream object, which can be seen from the source code of these two classes.
4. Using serialization function
In the Java API, the object serialization interface is primarily provided by two classes: Objectoutputstream,objectinputstream.
In order to satisfy the different requirements, such as saving to file, memory, transmission over the network, the object is serialized and saved in the Stream object. When a different stream object is provided, it is stored in the corresponding stream object after serialization. For example, provide FileOutputStream and FileInputStream, save in the file, provide Bytearrayoutputstream, Bytearrayinputstream, is saved in memory.
Since the Java API already provides the relevant code to implement serialization, it is easy to use serialization in most cases. For example:
Save object:
?
1 2 3 4 5 6 |
//创建一个流对象,比如文件输出流对象 FileOutputStream underlyingStream = new FileOutputStream( "C:\\temp\\test" ); //用刚才的文件流,创建一个对象序列化输出流 ObjectOutputStream serializer = new ObjectOutputStream(underlyingStream); //使用该流的输出函数,将对象序列化后保存到文件流中,也就是保存到了对应文件中。 serializer.writeObject(serializableObject); |
To read the object, the operation is exactly one by one corresponding to the Save:
?
1 2 3 4 5 6 |
//创建一个流对象,比如文件输入流对象 FileInputStream underlyingStream = new FileInputStream( "C:\\temp\\test" ); //用刚才的文件流,创建一个对象序列化输入流 ObjectInputStream deserializer = new ObjectInputStream(underlyingStream); //使用该流的输入函数,将文件中保存的对象读取到内存中,并创建相应对象。 Object deserializedObject = deserializer.readObject( ); |
5. What kind of class can be serialized
Not all classes need to be serialized, such as the thread class, and there is no need to save the information in these classes. This is also one of the reasons why serialization has not become an internal feature of Java. Therefore, if a class requires serialization, the serializable or Externalizable interface must be implemented in the class definition.
such as the character class in the Java API:
?
0 S |
public final class Character implements java.io.Serializable, Comparable<Character> |
6. Further content
Further content such as transient keywords, custom serialization mechanisms, serialized versioning, etc., please refer to the following articles:
Java RMI Chapter serialization by William Grosso
Http://oreilly.com/catalog/javarmi/chapter/ch10.html
Discover the secrets of the Java serialization API by Todd Greanier
Http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html
Ivor Horton ' s Beginning Java (Java 7 Edition by Ivor Horton) Chapter serializing Object
http://cn.bing.com/search?q=beginning+java+java+7+edition&go=& qs=as&form=qbre&pq=beginning+ java+java&sc=2-19&sp=1&sk=
Understanding of Java Serialization (serialization)