简体   繁体   English

为什么serialVersionUID对于可序列化类的类型很长

[英]Why serialVersionUID is of type long for Serializable classes

Does that mean that we can have all that number of versions for a single class? 这是否意味着我们可以为一个类拥有所有数量的版本? I assume not. 我认为不是。 I'm seeing it to be much shorter primitive. 我看到它的原始性要短得多。

What is a serialVersionUID and why should I use it? 什么是serialVersionUID,为什么要使用它? doesn't state why the guy implemented this mecanism has chosen it to be a long value. 没有说明为什么实施该机制的家伙选择它作为长期价值。

The type of serialVersionUID should be long because that is how it was specified . serialVersionUID的类型应该很long因为这是它的指定方式 See http://docs.oracle.com/javase/8/docs/platform/serialization/spec/class.html (specifically section 4.6) 请参阅http://docs.oracle.com/javase/8/docs/platform/serialization/spec/class.html (特别是第4.6节)

The reason that the UID is 64 bits is that the risk of accidental UID collision would be too high if (say) 32 bit values were used. UID为64位的原因是,如果使用(例如)使用32位值,则意外UID冲突的风险会很高。

The default serial version UID generation scheme creates a hash based on classes methods and fields. 默认的串行版本UID生成方案基于类方法和字段创建哈希。 There is a small, but non-zero, that two different classes will have the same default UID. 有一个很小但非零的值,即两个不同的类将具有相同的默认UID。 If this happens, then the deserializer may not notice that serialized form of the object is incompatible with the slass we are trying to deserialize to. 如果发生这种情况,那么反序列化程序可能不会注意到该对象的序列化形式与我们要反序列化的对象不兼容。 Bad things would then happen. 坏事就会发生。

If 32 bit UIDs were used, the chance that two incompatible classes had the same UID would be one in 2 32 . 如果使用32位UID,则两个不兼容的类具有相同的UID的机会是2 32之一 That is about one chance in 4 billion. 那大约是40亿的机会。 That is too large a chance. 机会太大了。 With 64 bit UID values, the change is one in 2 64 . 对于64位UID值,更改为2 64之一 That is about one chance in 16 quadrillion. 那大约是16万亿次的机会。 That was deemed (by the designers) to be an acceptably small probability. (设计者)认为这是一个可接受的小概率。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 serialversionuid是静态的和最终的,但是为什么可以序列化 - serialversionuid is static and final but then why is it serializable 在实现 Serializable 的类中声明私有 static final long serialVersionUID = xxxxxxxxL 有什么区别 - What is the difference of declaring private static final long serialVersionUID = xxxxxxxxL in classes that implements Serializable 为什么Serialversionuid在Java中很长? - Why is Serialversionuid long in java? 可序列化的类GuiLote没有声明类型为long的静态最终serialVersionUID字段 - The serializable class GuiLote does not declare a static final serialVersionUID field of type long 可序列化的类X没有声明类型为long的静态最终serialVersionUID字段 - The serializable class X does not declare a static final serialVersionUID field of type long 可序列化类Employee不声明long类型的静态最终serialVersionUID字段 - The serializable class Employee does not declare a static final serialVersionUID field of type long 可序列化的类NodeListImpl没有声明类型为long的静态最终serialVersionUID字段 - The serializable class NodeListImpl does not declare a static final serialVersionUID field of type long Serializable的SerialVersionUID - SerialVersionUID for Serializable GWT RemoteServiceServlet-为什么可序列化以及如何处理serialVersionUID? - GWT RemoteServiceServlet - Why Serializable and how to handle serialVersionUID? Java:可序列化的内部和匿名类是否应具有Seri​​alVersionUID? - Java: Should serializable inner & anonymous classes have SerialVersionUID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM