serialVersionUID的作用

Posted roostinghawk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了serialVersionUID的作用相关的知识,希望对你有一定的参考价值。

Java中serialVersionUID的作用

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

一言以蔽之,就是为了在反序列化时验证数据发送者,防止中间有人篡改

 

参考:

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/Serializable.html

https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it

以上是关于serialVersionUID的作用的主要内容,如果未能解决你的问题,请参考以下文章

serialVersionUID的作用

serialVersionUID作用

serialVersionUID的作用

转:serialVersionUID作用

serialVersionUID的作用

serialVersionUID介绍与作用