zoukankan      html  css  js  c++  java
  • serialVersionUID的作用

    当你一个类实现了Serializable接口,如果没有定义serialVersionUID,Eclipse会提供这个提示功能告诉你去定义之。

    serialVersionUID 用来表明类的不同版本间的兼容性。如果你修改了此类, 要修改此值。否则以前用老版本的类序列化的类恢复时会出错。

    Q: What is a serialVersionUID?

    A: The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in anInvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

    private static final long serialVersionUID = 6201378234876555585L;

      SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization.You must declare serialVersionUID because it give us more control.

    Q:What happen if i don't want to declare a serialVersionUID

    A:If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification.

      However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.

    Q:Why should I use serialVersionUID?

    A:Regardless of what serialized form you choose, declare an explicit serial version UID in every serializable class you write. This eliminates the serial version UID as a potential source of incompatibility (Item 74). There is also a small performance benefit. If no serial version UID is provided, an expensive computation is required to generate one at runtime.

  • 相关阅读:
    常用的排序算法的时间复杂度和空间复杂度
    VMWare虚拟机设置固定IP上网方法
    哈希表的冲突处理方法
    lua ipairs和pairs的区别 .
    NP完全问题
    java+selenium+new——文本框的输入、清空——按钮的单击
    java+selenium+new——操作单选按钮——使用list容器进行遍历
    java+selenium+new——对某个元素进行双击(举例示例操作)
    java+selenium+new——操作单选下拉列表——打印每一个选项——3种方法选中某一个选项——select类
    java+selenium+new——对当前浏览器窗口进行截屏,保存到指定文件夹,并取名
  • 原文地址:https://www.cnblogs.com/taiguyiba/p/8577315.html
Copyright © 2011-2022 走看看