Java 中的串行版本 ID 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2258676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is Serial Version ID in Java?
提问by GuruKulki
Possible Duplicates:
Why should I bother about serialVersionUID?
what is a serial version id?
When i created a new class extending java.util.logging.Level in Eclipse, It asked me to add a default or generated serial version ID. I just added blindly without knowing what it is and why I have to add it.
当我在 Eclipse 中创建一个扩展 java.util.logging.Level 的新类时,它要求我添加默认或生成的串行版本 ID。我只是盲目地添加,不知道它是什么以及为什么我必须添加它。
Can anybody tell me what is it and why it its required.
谁能告诉我它是什么以及为什么需要它。
回答by tangens
The Serial Version ID is used when serializing and deserializing an object. Java recognizes if the bytes you want to deserialize match the local class version. If not it will throw an exception.
序列化和反序列化对象时使用序列版本 ID。Java 会识别要反序列化的字节是否与本地类版本匹配。如果不是它会抛出异常。
This is important when doing RMI or persisting object structures.
这在执行 RMI 或持久化对象结构时很重要。
There's a very good description about serializing in the javadoc of Serializable.
在Serializable的javadoc 中有关于序列化的很好的描述。
回答by metismo
It is the unique identifier for the class, used for serialization.
它是类的唯一标识符,用于序列化。
It is wise to declare it if you're serializing asap because if you don't declare one then when changing the class it will get a different one generated automatically and the serialization will stop working.
如果您要尽快序列化,那么声明它是明智的,因为如果您不声明一个,那么在更改类时,它将自动生成一个不同的类,并且序列化将停止工作。
A good reference is here: http://c2.com/ppr/wiki/JavaIdioms/AlwaysDeclareSerialVersionUid.html
一个很好的参考在这里:http: //c2.com/ppr/wiki/JavaIdioms/AlwaysDeclareSerialVersionUid.html