Java序列化中的serialVersionUID有什么用?
如果一个实现了Serializable的类没有serialVersionUID属性,IDE(比如Eclipse)通常会报这样一个warning:
The serializable class Foo does not declare a static final
serialVersionUID field of type long
那这个serialVersionUID是做什么用的呢?可以看看JDK中Serializable接口的注释。
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 an {@link InvalidClassException}. A serializable class can
declare its own serialVersionUID explicitly by declaring a field
named"serialVersionUID"that must be static, final, and
of typelong:serialVersionUID 表示可序列化类的版本,在反序列化对象时,用来确认序列化与反序列化该对象所使用的类的版本是否兼容。
如果类的版本不一致,那么反序列化将不能正常进行,抛出InvalidClassException。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
unexpectedInvalidClassExceptions 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 theprivatemodifier
where possible, since such declarations apply only to the immediately
declaring class--serialVersionUID fields are not useful as inherited
members. Array classes cannot declare an explicit serialVersionUID,
so they always have the default computed value, but the requirement
for matching serialVersionUID values is waived for array classes.如果一个可序列化的类没有包含serialVersionUID,运行时会根据这个类的特征自动计算出一个serialVersionUID。
那么,为什么不能用默认的这个实现呢,似乎更省事?
因为不同的编译器实现会导致同一个类的源代码文件,被计算出不同的serialVersionUID.
StackOverflow上有一个类似的问题:
http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it
点击下面这个链接,可以查看更多关于Serializable接口的信息:
https://github.com/ZhaoX/jdk-1.7-annotated/blob/master/src/java/io/Serializable.java
Java序列化中的serialVersionUID有什么用?的更多相关文章
- Java序列化中的SerialVersionUid
版权声明:本文为博主fbysss原创文章,转载请注明出处 作者:fbysssmsn:jameslastchina@hotmail.com blog:blog.csdn.NET/fbysss声明:本文 ...
- 理解Java序列化中的SerialVersionUid
一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...
- 2017.12.22 Java序列化中你不知道的事(一)
Java 序列化简介 Java 对象序列化是 JDK 1.1 中引入的一组开创性特性之一,用于作为一种将 Java 对象的状态转换为字节数组,以便存储或传输的机制,以后,仍可以将字节数组转换回 Jav ...
- 关于java序列化中的一个细节
java序列化机制的可以参考很多资料了,最近在看的时候发现了一些问题. 1. 默认的序列化机制,很多书里讲到序列化类只序列化类名,实例变量,不会实例化类变量(static)和瞬态变量(transien ...
- 【Java】Java 序列化的高级认识
如果你只知道实现 Serializable 接口的对象,可以序列化为本地文件.那你最好再阅读该篇文章,文章对序列化进行了更深一步的讨论,用实际的例子代码讲述了序列化的高级认识,包括父类序列化的问题.静 ...
- 深入理解JAVA序列化
如果你只知道实现 Serializable 接口的对象,可以序列化为本地文件.那你最好再阅读该篇文章,文章对序列化进行了更深一步的讨论,用实际的例子代码讲述了序列化的高级认识,包括父类序列化的问题.静 ...
- java基础(十)-----Java 序列化的高级认识
将 Java 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列化的类需要实现 Serializable 接口,使用 ...
- Java序列化系列教程(下)
一引言 将 Java 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列化的类需要实现 Serializable 接口 ...
- 关于Java序列化和Hadoop的序列化
import java.io.DataInput; import java.io.DataOutput; import java.io.DataOutputStream; import java.io ...
随机推荐
- 充分利用 UE4 中的噪声
转自:https://www.unrealengine.com/zh-CN/blog/getting-the-most-out-of-noise-in-ue4 UE4 推出基于材质的程序式噪声已经有一 ...
- Codeforces Round #177 (Div. 1) 题解【ABCD】
Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...
- 解决 The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
到 http://tomcat.heanet.ie/native/ 下载最新的tcnative-1.dll放到相应目录即可,我目前下载的是 http://tomcat.heanet.ie/native ...
- web项目中加入struts2、spring的支持,并整合两者
Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...
- SAP-GR/IR的理解
SAP-GR/IR的理解 http://shousitukyou.blog.163.com/blog/static/13868005820109127046318/ GR/IR 1,采购的an i ...
- c++中的&
变量的前面表示取变量地址赋值给指针, 如:int a = 0; int *pa = &a;类型后面表示引用,引用即变量的替身. int a = 0; int &ref = a;操作re ...
- juniper 550M訪问自身公网IP回流内部IP
拓扑图示意: 网关设备juniper 550M, untrust 区: 公网地址段22.22.22.22/29 trust区: 内部员工PC地址:172.16.4.x /24 trust区: ...
- Eclipse调试时Application XXX is waiting for the debugger to attach的提示
原文链接: http://blog.csdn.net/star_huang/article/details/7678845 最近Eclipse调试时总是出现Application XXX is wa ...
- visual studio 2013使用技巧
去掉 引用提示 文本编辑器=>所有语言=>codelens visual studio 由于以前的函数求值超时,函数求值被禁用.必须继续执行才能重新启用函数求值 visual studio ...
- jQuery UI--jquery-autohide解读
// jQuery Autohide v1.0.2 // (c) 2014 Alex Taujenis // MIT License (function($) { return $.fn.autohi ...