1. What are Java serialization and deserialization?
Java serialization refers to the process of converting Java objects into byte sequences, while Java deserialization refers to the process of restoring the byte sequences into Java objects:
- Serialization: The most important purpose of object serialization is to ensure the integrity and transitiveness of the object when passing and saving the object. Serialization is the conversion of objects into an ordered byte stream for transmission on the network or stored in a local file. The core function is to preserve and rebuild the object state.
- Deserialization: The client obtains the serialized object byte stream from a file or on the network, and reconstructs the object by deserialization based on the object status and description information stored in the byte stream.
2. Why do we need serialization and deserialization?
Why serialization is what is the benefit of serialization? What are the advantages of serialization? So we need to serialize.
1: Object serialization can implement distributed objects.
Main applications such as RMI (i.e., remote call Remote Method Invocation) use object serialization to run services on remote hosts, just like when running objects on local machines.
2: Java object serialization not only retains the data of one object, but also recursively saves the data of each object referenced by the object.
The entire object hierarchy can be written into a byte stream, saved in a file, or passed on a network connection. Using object serialization, you can perform "deep copying" of objects, that is, copying the object itself and the referenced object itself. Serializing an object can result in the entire sequence of objects.
Three: Serialization can write classes in memory to files or databases.
For example: Serialize a class and save it as a file. The next time you read it, you only need to deserialize the data in the file to restore the original class to memory. The class can also be serialized into streaming data for transmission.
In general, it is to convert an instantiated class into a file storage. The next time you need to instantiate, just deserialize the class to instantiate and retain all variables and states in the class during serialization.
4: Objects, files, and data have many different formats, and it is difficult to transmit and save uniformly.
After serialization, it will be a byte stream. No matter what it originally was, it can become the same thing, and it can be transmitted or saved in a common format. After the transmission is completed, if it needs to be used again, deserialization and restore, so that the object is still an object, a file is still a file.
3. How to implement Java serialization and deserialization
First, we need to serialize the preparations and implement the Serializabel interface
For example: We want the name and age in the Person class to be serialized
import ; public class Person implements Serializable { //This class can be serialized private String name; private int age; public Person(String name, int age) { = name; = age; } public String toString() { return "Name:" + + ",age" + ; } }
Then: we serialize name and age (that is, convert these two objects into binary, which is understood as "breaking")
package ; import ; import ; import ; public class ObjectOutputStreamDemo { //Serialization public static void main(String[] args) throws Exception { //Serialize the specified file path File file = new File("D:" + + ""); ObjectOutputStream oos = null; //Decorative flow (flow) oos = new ObjectOutputStream(new FileOutputStream(file)); //Instantiated class Person per = new Person("Zhang San", 30); (per); //Serialize class objects (); } }
The above is the detailed content of three common problems in Java serialization. For more information about Java serialization, please pay attention to my other related articles!