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 ...
随机推荐
- sublime Text Pastry使用
来源: https://github.com/duydao/Text-Pastry/wiki/Examples Using a text list Using the Clipboard Clip ...
- WPF 的datagrid 列名中没有显示下划线是怎么回事?
引用:http://www.cnblogs.com/sqzhuyi/archive/2011/07/26/sqlserverclient.html#2831497 功能介绍:输入查询语句,显示列表 出 ...
- 在redis一致性hash(shard)中使用lua脚本的坑
redis 2.8之前的版本,为了实现支持巨量数据缓存或者持久化,一般需要通过redis sharding模式来实现redis集群,普遍大家使用的是twitter开源的Twemproxy. twemp ...
- ECSHOP始终显示全部分类方法
商品分类树需要始终显示所有类别,默认的Ecshop的显示方式为在当前产品页面只显示当前的产品所在的同级及下级分类,这就导致当点开某个产品或者子分 类的时候全局的分类树就不见了. 其实修改的方法很简单. ...
- Codeforces 346C Number Transformation II 构造
题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...
- 送给大家一个安卓版的easyradius短信提示客户端
好像木有写博客了,送大家小软件,后期会适当更新 主要是方便一些用手机给用户发送到期短信的用户 下载地址: http://www.yss58.com/yss58
- [原创]android开源项目源码解析(一)----CircleImageView的源码解析
CircleImageView的代码很简洁,因此先将此工程作为源码解析系列的第一篇文章. 解析说明都在代码里了. /* * Copyright 2014 - 2015 Henning Dodenhof ...
- Nginx upstream 长连接
原文: http://bollaxu.iteye.com/blog/900424 Nginx upstream目前只有短连接,通过HTTP/1.0向后端发起连接,并把请求的"Connecti ...
- 搬家至个人独立博客virson.cn
最近正在将博客园的文章搬到自己的独立博客,以后基本上不会在博客园更新文章了,欢迎光临我的新博客:www.virson.cn,博客内容持续更新中……
- synchronized关键字,Lock接口以及可重入锁ReentrantLock
多线程环境下,必须考虑线程同步的问题,这是因为多个线程同时访问变量或者资源时会有线程争用,比如A线程读取了一个变量,B线程也读取了这个变量,然后他们同时对这个变量做了修改,写回到内存中,由于是同时做修 ...