java - 为什么我们在序列化过程中使用 serialVersionUID

标签 java serialization

<分区>

在我的采访中,我有一个问题如何实现序列化,我给出的答案是

public class SerializationSample {
    public static void main(String args[]) {
        ...........
        FileOutputStream fos = new FileOutputStream(outFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(serializableObject);
        ............
}
class SerializationBox implements Serializable {
    ............
    ............
}

但是面试官说,这会序列化整个对象,他问了两个问题

  1. 如果我想序列化单个对象,您将如何编写代码?
  2. 为什么我们在序列化过程中使用 long_integer id?

请任何人帮助我找到这个问题的答案

最佳答案

让我引用来自similar and already answered question的精华

The serialVersionUID represents your class version, and you should increment it if the current version of your class is not backwards compatible with its previous version.

automatically-generated UID is generated based on a class name, implemented interfaces, and all public and protected members. Changing any of these in any way will change the serialVersionUID. So you don't need to mess with them only if you are certain that no more than one version of the class will ever be serialized (either across processes or retrieved from storage at a later time).

我推荐你访问that link并一如既往地搜索网络。

希望对您有所帮助。

关于java - 为什么我们在序列化过程中使用 serialVersionUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870812/

相关文章:

java - 为什么函数会在循环中被多次调用?

java - 如何解析以下JSON

java - 查找数组中任何重复的元素

Java 序列化没有按我的预期工作

Kotlinx 序列化 - 如何为未知字段名称编写数据类

php - 如何用php反序列化这些数据?

Java 泛型 : Syntax Explanation

没有 X 的 Java web 启动

java - 确定序列化对象的类型

java - OSGI 中 Akka 2.2.x 远程中的 ClusterMessageSerializer ClassNotFoundException