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.

  • 相关阅读:
    实现对DataGird控件的绑定操作
    EasyUI-datagrid中load,reload,loadData方法的区别
    easui Pagination Layout
    easyUI datagrid 排序
    jQuery EasyUI教程之datagrid应用
    solr入门之多线程操作solr中索引字段的解决
    序列自相关矩阵的计算和分析
    UVa 12403
    滑动窗体的最大值(STL的应用+剑指offer)
    bzoj2101【Usaco2010 Dec】Treasure Chest 藏宝箱
  • 原文地址:https://www.cnblogs.com/taiguyiba/p/8577315.html
Copyright © 2011-2022 走看看