java 序列化 uid_关于java:为什么序列化需要在实现的类中使用串行版本的UID?...

为什么序列化需要在实现的类中使用串行版UID?

来自java.ioSerializable的文档:

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 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

你问为什么UID?

因此它知道两个序列化对象是否彼此兼容。 您可能已经更改了接口/方法/等。

UID是(公认的相当蹩脚)合同,它说这两个对象实际上是同一类型的对象。

你问为什么需要这个串口版UID?

Why does serialization require a

serial version UID in implemented

class?

它根本不需要它。 这是一个可选功能。

可选的意思是它将在运行时自动生成序列化......它很简单,你要么明确地给它,或者编译器会生成串行版本的UID。

@ Anandj.Kadhi否.JVM会生成它,特别是ObjectOutputStream和朋友。 它仍然是可选的,它仍然不是由编译器生成的。 不要在这里发布错误信息。

在规范中提到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意味着不同的编译器实现会导致不同的serailVersionUID,因此我提到了编译器。 请刷新你的信息

serialVersionUID用作Serializable类中的版本控件。 如果您没有显式声明serialVersionUID,JVM将根据您的Serializable类的各个方面自动为您完成,如Java(TM)对象序列化规范中所述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值