为什么序列化需要实现类中的序列版本 UID? [英] Why does serialization require a serial version UID in implemented class?
问题描述
为什么序列化需要实现类中的序列化版本 UID?
Why does serialization require a serial version UID in implemented class?
推荐答案
来自 java.ioSerializable
的文档:
From the doc for java.ioSerializable
:
序列化运行时为每个可序列化类关联了一个版本号,称为 serialVersionUID,在反序列化期间使用该版本号来验证序列化对象的发送方和接收方是否已为该对象加载了与序列化兼容的类.如果接收方为对象加载了一个与相应发送方的类具有不同 serialVersionUID 的类,则反序列化将导致 InvalidClassException
.一个可序列化的类可以通过声明一个名为 "serialVersionUID"
的字段来显式声明它自己的 serialVersionUID,该字段必须是 static、final 和 long 类型
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!