java serialization interpretation

As we all know, the object class with the termination of the program will be destroyed garbage collector. If you want to call the class without re-creating the object, how to do? This can convert the sequence data to byte stream.
Object serialization is a state of the object for converting the byte stream to the process, it can be saved to a disk file or sent to any other through a network; creating an object from the byte stream is called opposite deserialization . The byte stream created are platform-independent, in a sequence of the target platform can deserialize on different platforms.
How to make Java class can be serialized?
By implementing the java.io.Serializable interfaces , can be enabled in the sequence of Java classes. It is a marker interface, meaning that it does not contain any method or field, only to identify semantic serializable.

Why should serialization and de-serialization?
In Java, we can create objects in a variety of ways, as long as the object has not been recovered and we can reuse this object. However, we create out of these objects are present in the JVM heap (stack) memory only when the JVM is running, only those objects that may exist. Once the JVM is stopped, these objects will disappear ;
but in a real scenario, we need to be persistent down these objects , and the object will re-read it when needed, Java serialization can help us achieve this Features.
Object serialization mechanism (object serialization) is a java language object persistence built manner, by the target sequence, the object may change the state information is stored byte array , and may be needed when there is this byte array by deserialize way to convert an object, object serialization can be switched easily between the active object and the byte array in the JVM (stream).
In JAVA, serialization and deserialization of the objects to be widely applied to the RMI (remote method invocation) and network transmissions;


java developer for the convenience of users are serialized, then developed with serialization function api api users to complete the serialization.

Java classes to enable serialization capabilities by implementing java.io.Serialization interfaces , classes do not implement this interface will not be any state or information to serialize or deserialize. All subtypes sequences can class are serializable. Serialization interface has no method, or field, only to identify the semantic serializable .
Serialized and deserialized by the following code:

package common.lang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableDemo1 {

    public static void main(String[] args) throws Exception, IOException {
        //初始化对象
        User1 user = new User1();//声明:user1 是我们创建了一个类
        user.setName("yaomy");
        user.setAge(23);
        System.out.println(user);
        //序列化对象到文件中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("template"));
        oos.writeObject(user);
        oos.close();
        //反序列化
        File file = new File("template");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        User1 newUser = (User1)ois.readObject();
        System.out.println(newUser.toString());
    }
}

Guess you like

Origin blog.csdn.net/qq_33458689/article/details/95318447