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

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

问题描述

如何对象.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 content or address of the object?

hashCode()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).

System.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() 是否返回对象的引用?

Does identityHashCode() return the reference of the object?

没有.它返回一个 int,而 int 不能保存引用.

No. It returns an int, and an int cannot hold a reference.

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 if 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() 方法是一个非最终实例方法,应该在任何重写了 equals(Object) 的类中重写.相比之下,identityHashCode(Object) 是一个 static 方法,因此不能被覆盖.

  • 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天全站免登陆