从 Java 5 开始不再需要serialVersionUID?
我最近读到一条评论,说从 Java 5 开始,不再需要使用serialVersionUID 来使同一类的不同版本兼容序列化/反序列化。这是正确的吗?也许它与协变返回类型混淆了。所以想和大家一起看看。
I recently read a comment saying that usage of serialVersionUID to make different versions of the same class compatible for serialization/deserialization is no longer needed from Java 5 onwards. Is this correct? Maybe it got mixed-up with covariant return types. So wanted to check it out with you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Java 1.4 开始,您就可以使用 XMLEncoder< /a> 用于序列化,这会忽略任何serialVersionUID,就像大多数其他序列化库一样。
但是,如果您使用ObjectOutputStream,其serialVersionUID的用法并没有改变。
Since Java 1.4 you have been able to use XMLEncoder for Serialisation and this ignores any serialVersionUID, as does most other serialisation libraries.
However if you use ObjectOutputStream, its usage of serialVersionUID has not changed.
JavaDocs for Java 5 仍然指出需要它:
The JavaDocs for Java 5 still states that it is needed: