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 - 如何在 Spring MVC 中使用单个 View 执行所有 CRUD 操作?

java - Spring Boot 应用在 Eclipse 上输出 logo 和版本后突然停止

c# - 如何反序列化对结构等效项的引用集合?

c# - XML 序列化注释

ruby - 如何在 ruby​​ 2.2.4 (Windows) 中序列化和导出数据?

java - 只作为一名玩家运行命令?我的世界插件开发

java - HTMLUnit 使用 JavaScript 代码执行表单

java - 你好,我正在努力理解这段代码中 if 语句的 O 表示法运行时

java - 我们如何在 Redis 中存储和检索可序列化的 Java 对象作为值

serialization - Hadoop 序列化器未找到异常