由于版本8
Java具有
value-based classes的概念.这是为了准备未来的版本,这将很有可能允许
value types的定义.两个定义/描述都提到序列化(由我添加的粗体):
关于现有的基于价值的课程:
A program may produce unpredictable results if it attempts to distinguish two references to equal values of a value-based class, whether directly via reference equality or indirectly via an appeal to synchronization, identity hashing, serialization, or any other identity-sensitive mechanism.
关于未来价值类型:
The default identity-based hash code for object, available via System.identityHashCode, also does not apply to value types. Internal operations like serialization which make identity-based distinctions of objects would either not apply to values (as they do not apply to primitives) or else they would use the value-based distinction supplied by the value type’s hashCode method.
因为未来的JVM实现可能不会使用基于值的类的对象头和引用指针,所以有一些限制是明确的. (例如,不锁定JVM不能维护的身份,锁定的引用可以被删除并替换为另一个,这将释放锁定并且将导致死锁).
但是我不知道序列化如何发挥作用.为什么它被认为是“身份敏感机制”?为什么“使基于身份的对象区别”?