为什么序列化需要在实现的类中使用串行版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)对象序列化规范中所述。