System.identityHashCode(Object o)
时间: 2024-05-16 22:16:47 浏览: 12
System.identityHashCode(Object o) 方法返回给定对象的哈希码,与该对象的内存地址相关。它不同于 Object.hashCode() 方法,后者返回的是根据对象的内容计算出来的哈希码。因此,即使两个对象的内容相同,它们的 identityHashCode 也可能不同。
下面是一个示例:
```
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(System.identityHashCode(str1)); // 输出: 460141958
System.out.println(System.identityHashCode(str2)); // 输出: 1163157884
```
在这个例子中,`str1` 和 `str2` 内容相同,但是它们在内存中的地址不同,因此它们的 identityHashCode 也不同。
相关问题
system.identityHashcode
The `System.identityHashCode()` method is a built-in method in Java that returns the hashCode value of an object. The hashCode value is a unique integer value assigned to every object in Java. This value is used by the hash-based data structures in Java like HashSet, HashMap, etc. to determine the location of an object in their internal data structure.
The `System.identityHashCode()` method differs from the `Object.hashCode()` method in that it returns the same hashCode value for the same object every time it is called, regardless of any changes made to the object's state. The `Object.hashCode()` method, on the other hand, can return different hashCode values for the same object if its state changes.
It is important to note that the `System.identityHashCode()` method does not guarantee that the returned hashCode value will be unique across all objects in the Java runtime environment. However, it does guarantee that the hashCode value will be unique for each object within the current execution of the program.
java.lang.System类的方法
Java中的System类提供了许多有用的方法,例如:
1. `currentTimeMillis()`:返回当前时间(以毫秒为单位)。
2. `arraycopy(Object src, int srcPos, Object dest, int destPos, int length)`:将src数组中的一部分复制到dest数组中。
3. `exit(int status)`:退出JVM并返回一个整数状态码。
4. `getProperty(String key)`:获取指定的系统属性。
5. `getenv(String name)`:获取指定的环境变量。
6. `gc()`:运行垃圾回收器。
7. `setIn(InputStream in)`:重新设置标准输入流。
8. `setOut(PrintStream out)`:重新设置标准输出流。
9. `setErr(PrintStream err)`:重新设置标准错误输出流。
10. `identityHashCode(Object x)`:返回指定对象的哈希码值。
等等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)