Skip to content

Commit 9d9b55a

Browse files
committed
Merge branch 'main' of github.com:Snailclimb/JavaGuide
2 parents 818ccbc + fcdbe12 commit 9d9b55a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

docs/java/basis/java-basic-questions-02.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,18 @@ public boolean equals(Object anObject) {
425425

426426
![hashCode() 方法](https://oss.javaguide.cn/github/javaguide/java/basis/java-hashcode-method.png)
427427

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

0 commit comments

Comments
 (0)