Java学习札记10:Why generate long serialVersionUID instead of a simple 1L?

本文详细解释了serialVersionUID在Java序列化中的作用,包括为什么应该为实现Serializable接口的类显式定义serialVersionUID,如何避免因类结构变更导致的序列化兼容性问题,以及手动指定serialVersionUID与自动生成之间的区别。




一些答案:

1、

The idea behind using 1L is so that you increment it every time you change the class properties or methods.


2、

The "long" default of the serialVersionUID is the default value as defined by the Java Serialization Specification, calculated from the default serialization behaviour.

So if you add the default version number, your class will (de-)serialize faster as long as nothing has structurally changed, but you'll have to take care that if you change the class (add/remove fields) you also update the serial number.

If you do not have to be compatible to existing bit streams, you can just put 1L there and increment the version as needed when something changes. That is, when the default serialisation version of the changed class would be different from the default version of the old class.


3、

There is no runtime performance impact of allowing the serialversionUID to be generated automatically - it's generated at compile time by javac...if you decompile the class's bytecode, you'll actually see the variable statically in the bytecode.


4、

You absolutely should create a serialVersionUID every time you define a class that implements java.io.Serializable. If you don't, one will be created for you automatically, but this is bad. The auto-generated serialVersionUID is based on the method signatures of your class, so if you change your class in the future to add a method (for example), deserializing the "old" versions of the class will fail. Here's what can happen:

    1. Create the first version of your class, without defining the serialVersionUID. 

    2. Serialize an instance of your class to a persistent store; a serialVersionUID is automatically generated for you. 

    3. Modify your class to add a new method, and redeploy your application. 

    4. Attempt to deserialize the instance that was serialized in step 2, but now it fails (when it should succeed), because it has a different auto-generated serialVersionUID. 


5、

If you don't specify a serialVersionUID then Java makes one on the fly. The generated serialVersionUID is that number. If you change something in your class that doesn't really make your class incompatible with previous serialized verisons but changes the hash, then you need to use the generated very-large-number serialVersionUID (or the "expected" number from the error message). Otherwise, if you are keeping track of everything yourself, 1, 2, 3... is better.



问题出处:

http://stackoverflow.com/questions/888335/why-generate-long-serialversionuid-instead-of-a-simple-1l








评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值