When two processes are communicating remotely, they can send different types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object into a byte sequence to be routed over the network, and the receiver needs to revert the byte sequence back to the Java object.
The process of converting Java objects into byte sequences is called serialization of objects.
The process of restoring a byte sequence to a Java object is called deserialization of an object.
There are two main uses for serialization of objects:
1 The object's byte sequence is permanently saved to the hard disk, usually stored in a file;
2 transfer the byte sequence of the object on the network.
A Serialization APIs in the JDK class library
Java.io.ObjectOutputStream represents an object output stream whose WriteObject (object obj) method serializes the Obj object specified by the parameter and writes the resulting byte sequence to a target output stream.
Java.io.ObjectInputStream represents an object input stream whose ReadObject () method reads a sequence of bytes from a source input stream, deserializes them into an object, and returns them. 、
Only objects of classes that implement the serializable and Externalizable interfaces can be serialized. The Externalizable interface inherits from the serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, and the class that implements only the serializable interface can use the default serialization method.
Object serialization consists of the following steps:
1 Create an object output stream that can wrap a different type of target output stream, such as a file output stream;
2 The object is written through the WriteObject () method of the object output stream.
The steps to deserialize an object are as follows:
1 Create an object input stream that can wrap a different type of source input stream, such as a file input stream;
2 The object is read by the ReadObject () method of the object input stream.