java java序列化如何在内部工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17102207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:58:51  来源:igfitidea点击:

How java serialization works internally?

javaserialization

提问by Sam

I was reading about Java serialization and came to know that if Serializable interface is implemented then class is serialized.

我正在阅读有关 Java 序列化的内容,并了解到如果实现了 Serializable 接口,则类将被序列化。

But Serializable is a marker interface. Then how does JVM know with which methods serialization or de-serialization should be done?

但是 Serializable 是一个标记接口。那么JVM怎么知道应该用哪些方法进行序列化或反序列化呢?

As per my understanding the methods declared in an interface are called via polymorphism.

根据我的理解,接口中声明的方法是通过多态调用的。

I would give an example below to examplain my question.

我将在下面举一个例子来说明我的问题。

    public class MySerializable implements Serializable{

    public void serialize(){

      //Some code to serialize to a file output stream.
    }

    public void deSerialize(){

      //Some code to de-serialize to a file input stream.
    }


}

So now how JVM will call the methods serialize / deSerialize?

那么现在JVM将如何调用方法serialize / deSerialize?

And if I have to call them manually via code then why compiler should be let known that Serializable interface is implemented?

如果我必须通过代码手动调用它们,那么为什么应该让编译器知道实现了 Serializable 接口?

回答by Eldar Agalarov

Yes, Serializable interface don't have any methods to implement. If you want serialize object, your object must implement Serializable interface. Next, you can do serialization/deserialization procedures with your object, for example using ObjectOutputStream and ObjectInputStream objects.

是的,Serializable 接口没有任何方法可以实现。如果你想序列化对象,你的对象必须实现 Serializable 接口。接下来,您可以对您的对象执行序列化/反序列化过程,例如使用 ObjectOutputStream 和 ObjectInputStream 对象。

There are little example.

例子很少。

Class, which need to serialize/deserialize:

需要序列化/反序列化的类:

import java.io.Serializable;

public class ObjectToSerialize implements Serializable {

    private static final long serialVersionUID = 7526472295622776147L;

    private String firstName;
    private String lastName;

    public ObjectToSerialize(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return getFirstName() + " " + getLastName();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ObjectToSerialize that = (ObjectToSerialize) o;
        return firstName.equals(that.firstName) && lastName.equals(that.lastName);
    }
}

Class with serialization/deserialization methods:

具有序列化/反序列化方法的类:

import java.io.*;

public class ObjectSerialization {

    public static void saveObject(Serializable object, String path) throws IOException {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
            oos.writeObject(object);
        }
    }

    public static Object loadObject(String path) throws ClassNotFoundException, IOException {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
            return ois.readObject();
        }
    }

    public static void main(String[] args) {
        ObjectToSerialize object = new ObjectToSerialize("Eldar", "Agalarov");
        try {
            String path = "C:/object.bin";
            saveObject(object, path);
            System.out.println("Object serialized: " + object);

            ObjectToSerialize deserializedObject = (ObjectToSerialize) loadObject(path);
            System.out.println("Object deserialized: " + deserializedObject);
            System.out.println("They are equals: " + object.equals(deserializedObject));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Also read this guide about serialization

另请阅读有关序列化的本指南