如果一个实现了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 type long:

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
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. 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有什么用?的更多相关文章

  1. Java序列化中的SerialVersionUid

    版权声明:本文为博主fbysss原创文章,转载请注明出处 作者:fbysssmsn:jameslastchina@hotmail.com  blog:blog.csdn.NET/fbysss声明:本文 ...

  2. 理解Java序列化中的SerialVersionUid

    一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...

  3. 2017.12.22 Java序列化中你不知道的事(一)

    Java 序列化简介 Java 对象序列化是 JDK 1.1 中引入的一组开创性特性之一,用于作为一种将 Java 对象的状态转换为字节数组,以便存储或传输的机制,以后,仍可以将字节数组转换回 Jav ...

  4. 关于java序列化中的一个细节

    java序列化机制的可以参考很多资料了,最近在看的时候发现了一些问题. 1. 默认的序列化机制,很多书里讲到序列化类只序列化类名,实例变量,不会实例化类变量(static)和瞬态变量(transien ...

  5. 【Java】Java 序列化的高级认识

    如果你只知道实现 Serializable 接口的对象,可以序列化为本地文件.那你最好再阅读该篇文章,文章对序列化进行了更深一步的讨论,用实际的例子代码讲述了序列化的高级认识,包括父类序列化的问题.静 ...

  6. 深入理解JAVA序列化

    如果你只知道实现 Serializable 接口的对象,可以序列化为本地文件.那你最好再阅读该篇文章,文章对序列化进行了更深一步的讨论,用实际的例子代码讲述了序列化的高级认识,包括父类序列化的问题.静 ...

  7. java基础(十)-----Java 序列化的高级认识

    将 Java 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列化的类需要实现 Serializable 接口,使用 ...

  8. Java序列化系列教程(下)

    一引言 将 Java 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列化的类需要实现 Serializable 接口 ...

  9. 关于Java序列化和Hadoop的序列化

    import java.io.DataInput; import java.io.DataOutput; import java.io.DataOutputStream; import java.io ...

随机推荐

  1. VS2010链接TFS

    VS2010链接TFS源代码管理器 1.打开VS2010开发工具. 2.菜单视图===>>团队资源管理器 3.点击链接到团队项目 4.点击服务器 5.点击添加 6.输入TFS服务配置信息 ...

  2. centos7防火墙那些事

    转发设置 firewall-cmd --permanent  --add-forward-port=port=80:proto=tcp:toport=8080 firewall-cmd --perma ...

  3. windows批处理

    1.日期作为变量当做文件名的一部分. C:\Documents and Settings\Simon>echo %date%2008-09-09 星期二 C:\Documents and Set ...

  4. 使用tcpcopy拷贝线上流量压测测试环境

    tcpcopy项目地址:https://github.com/session-replay-tools/tcpcopy 作者地址:http://blog.csdn.net/wangbin579 1:环 ...

  5. Redis 数据结构之Keys

    这是Redis官方文档的keys列表 (1) set  key value--设置某个键为某个值 (2) get key -- 获取设置的值 (3)del key -- 删除设置的键 (4)expir ...

  6. linux下IM server搭建

    一步一步开始做. 附录: 一套开源协议:http://www.igniterealtime.org/index.jsp Proso:http://prosody.im/ 那谁网友的笔记http://w ...

  7. Linux文件系统性能优化 (转)

    http://blog.chinaunix.net/uid-7530389-id-2050116.html 由于各种的I/O负载情形各异,Linux系统中文件系统的缺省配置一般来说都比较中庸,强调普遍 ...

  8. git常用命令-基本操作

    git常用命令-基本操作 1)      新增文件 新增了Test_1.java git add Test_1.java git commit –m “新增了Test_1.java” git push ...

  9. 随机抽样一致性算法(RANSAC)示例及源代码

    作者:王先荣 大约在两年前翻译了<随机抽样一致性算法RANSAC>,在文章的最后承诺写该算法的C#示例程序.可惜光阴似箭,转眼许久才写出来,实在抱歉.本文将使用随机抽样一致性算法来来检测直 ...

  10. samba 服务器的搭建

    一,安装samba4 不要直接 yum install samba ,默认安装的是samba3版本,但这个版本有问题(open_rpc_pipe_p: copy_serverinfo failed这个 ...