1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-20 22:17:09 +08:00

Merge pull request #2001 from Erzbir/main

[docs fix]修正 hashCode() 方法的描述错误
This commit is contained in:
Guide 2023-04-28 21:19:55 +08:00 committed by GitHub
commit fcdbe12190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -425,7 +425,18 @@ public boolean equals(Object anObject) {
![hashCode() 方法](https://oss.javaguide.cn/github/javaguide/java/basis/java-hashcode-method.png)
`hashCode()`定义在 JDK 的 `Object` 类中,这就意味着 Java 中的任何类都包含有 `hashCode()` 函数。另外需要注意的是: `Object``hashCode()` 方法是本地方法,也就是用 C 语言或 C++ 实现的,该方法通常用来将对象的内存地址转换为整数之后返回。
`hashCode()` 定义在 JDK 的 `Object` 类中,这就意味着 Java 中的任何类都包含有 `hashCode()` 函数。另外需要注意的是: `Object``hashCode()` 方法是本地方法,也就是用 C 语言或 C++ 实现的。
> 注:
>
> 该方法在 **Oracle OpenJDK8** 中默认是 "使用线程局部状态来实现 Marsaglia's xor-shift 随机数生成", 并不是 "地址" 或者 "地址转换而来", 不同JDK/VM可能不同
>
> 在 **Oracle OpenJDK8** 中有六种生成方式 (其中第五种是返回地址), 通过添加VM参数: -XX:hashCode=4 启用第五种。
>
> 参考源码:
>
> https://hg.openjdk.org/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/globals.hpp, 1127行
> https://hg.openjdk.org/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/synchronizer.cpp, 537行开始
```java
public native int hashCode();