hashCode()和identityHashCode()如何在后端工作? [英] How do hashCode() and identityHashCode() work at the back end?

查看:95
本文介绍了hashCode()和identityHashCode()如何在后端工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 Object.hashCode() System.identityHashCode() 在后端工作? identityHashCode()是否返回对象的引用? hashCode()取决于?对象? ==运算符如何在后端工作。

How do Object.hashCode() and System.identityHashCode() work at the back end? Does identityHashCode() return the reference of the object? Does hashCode() depend on the ? of the object ? == operator how to work in back end.

hashCode()和<$ c之间有什么区别$ C> identityHashCode()?

推荐答案


Object.hashCode()和System.identityHashCode()如何在后端工作?

How do Object.hashCode() and System.identityHashCode() work at the back end?

假设它没有被覆盖, Object.hashCode()方法只调用 System.identityHashCode(this)

Assuming that it hasn't been overridden, the Object.hashCode() method simply calls System.identityHashCode(this).

系统的确切行为.identityHashCode(Object)取决于JVM实现。 (最近的Hotspot JVM的实际实现相当聪明,但我离题了。)

The exact behavior of System.identityHashCode(Object) depends on the JVM implementation. (The actual implementation on recent Hotspot JVMs is rather clever, but I digress.)


identityHashCode()返回对象的引用?

不。它返回 int ,而 int 无法保存引用。 (Duh!)

No. It returns an int, and an int cannot hold a reference. (Duh!)

identityHashCode 返回的整数可能与(a)对象的机器地址有关,或者它可能不是 1 identityHashCode() 返回的值保证在对象的生命周期内不会更改。这意味着GC重新定位一个对象(在 identityHashCode()调用之后),然后它不能使用新的对象地址作为标识哈希码。

That integer returned by identityHashCode may be related to the (a) machine address for the object, or it may not be1. The value returned by identityHashCode() is guaranteed not to change for the lifetime of the object. This means that of the GC relocates an object (after an identityHashCode() call) then it cannot use the new object address as the identity hashcode.


hashCode()是否依赖于对象? == 运算符如何在后端工作。

Does hashCode() depend on the ? of the object ? == operator how to work in back end.

这没有意义。没有? == ?== Java中的运算符。

This doesn't make sense. There is no ? == or ?== operator in Java.


hashCode()和identityHashCode()有什么区别?

What is the difference between hashCode() and identityHashCode()?

这部分解释如上。其他差异包括:

This is partly explained above. Other differences include:


  • hashcode()方法是非-final instance方法,应该在重写 equals(/ Object)的任何类中重写。相比之下, identityHashCode(Object)是一个静态方法,因此无法覆盖。

  • The hashcode() method is a non-final instance method, and should be overridden in any class where the equals(Object) is overridden. By contrast, identityHashCode(Object) is a static method and therefore cannot be overridden.

identityHashCode(Object)方法为您提供了一个对象的标识符,该标识符可以(理论上)用于除散列之外的其他内容和哈希表。 (不幸的是,它不是唯一的标识符,但 保证在对象的生命周期内永远不会改变。)

The identityHashCode(Object) method gives you a identifier for an object which can (in theory) be used for other things than hashing and hash tables. (Unfortunately, it is not a unique identifier, but it is guaranteed to never change for the lifetime of the object.)

1 - 对于当前一代的JVM,它根本与内存地址无关。请参阅@ bestsss的回答。

这篇关于hashCode()和identityHashCode()如何在后端工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆