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 ...
随机推荐
- LeakCanary 中文使用说明
http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/ LeakCanary 中文使用说明 分享到:新浪微博微信 10 May 2015 Leak ...
- Java 定时任务
control 类 Date nowDate = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH: ...
- 转:XBMC源代码分析
1:整体结构以及编译方法 XBMC(全称是XBOX Media Center)是一个开源的媒体中心软件.XBMC最初为Xbox而开发,可以运行在Linux.OSX.Windows.Android4.0 ...
- arulesSequences包做序列模式的关联分析
实验数据: 实验文件: ? 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 02 ...
- jQuery中position()与offset()区别
使用jQuery获取元素位置时,我们会使用position()或offset()方法,两个方法都返回一个包含两个属性的对象-左边距和上边距,它们两个的不同点在于位置的相对点不同. 可以看看下边的图: ...
- libevent
libevent doc example #include <event2/event.h> void cb_func(evutil_socket_t fd, short what, vo ...
- python 与数据结构
在上面的文章中,我写了python中的一些特性,主要是简单为主,主要是因为一些其他复杂的东西可以通过简单的知识演变而来,比如装饰器还可以带参数,可以使用装饰类,在类中不同的方法中调用,不想写的太复杂, ...
- Python从内存中使用编译后的模块
在Windows编程的时候,有些时候,我们经常会要使用一些非常规的方法,比如说从内存中加载DLL,然后使用DLL中的函数.于是就思索在用Python的时候是否能够将几个编译好的Pyc合并成一个,然后使 ...
- Android开发(二十五)——Android上传文件至七牛
设置头像: Drawable drawable = new BitmapDrawable(dBitmap); //Drawable drawable = Drawable.createFromPath ...
- iSAC测试报告
iSAC测试报告 测试码流:24k bit/s 测试环境:三星i9250 CPU 1.2G*2 ram:1G TI芯片 OMAP 4460 双核1.2GHz MOTO ME722 CPU ...