爪哇。lang.System。identityHashCode()是一种方法,用于为默认方法hashCode()返回的任何给定对象返回相同的哈希代码。此外,对于每个具有空引用的哈希代码,都会返回零。
null
需要记住的要点:
- 默认情况下,每个类都隐式或显式地提供hashCode()方法
- hashcode通常是从任何对象生成的数字,它允许在哈希表中快速存储或检索对象。
- 在Java中,hashCode()默认为本机方法,这意味着当该方法直接在JVM的本机代码中实现时,该方法有一个修饰符“native”。
- 用于将存储在类实例中的所有数据消化为单个哈希值,即32位有符号整数。
语法:
public static int identityHashCode(Object x)
参数: 参数 十、 属于哈希类型,并指需要计算的哈希代码。
返回值: 此方法返回哈希代码。
下面的程序演示了java的使用。lang.System。identityHashCode()方法。
项目1:
// Java program to demonstrate working
// of java.lang.System.identityHashCode() method.
import
java.lang.*;
import
java.io.*;
public
class
SystemCode1 {
public
static
void
main(String[] args)
throws
Exception
{
File filename1 =
new
File(
"Welcome"
);
File filename2 =
new
File(
"Welcome"
);
File filename3 =
new
File(
"Geek"
);
File filename4 =
new
File(
"World"
);
// Returns the HashCode
int
returnvalue1 = System.identityHashCode(filename1);
System.out.println(returnvalue1);
// Returns different HashCode for same filename
int
returnvalue2 = System.identityHashCode(filename2);
System.out.println(returnvalue2);
// Returns the HashCode
int
returnvalue3 = System.identityHashCode(filename3);
System.out.println(returnvalue3);
// Returns the HashCode
int
returnvalue4 = System.identityHashCode(filename4);
System.out.println(returnvalue4);
}
}
输出:589431969 1252169911 2101973421 685325104
说明: 在上面的程序中,一个对象生成不同的哈希代码或数字,即使它们具有相同的名称。在这里,我们可以看到前两个术语是相同的,即“欢迎”,但我们有两个不同的值,分别是
- 589431969
- 1252169911
分别为第一和第二 欢迎
项目2:
// Java program to demonstrate working
// of java.lang.System.identityHashCode() method.
import
java.lang.*;
import
java.io.*;
public
class
SystemCode2 {
public
static
void
main(String[] args)
throws
Exception
{
File filename1 =
new
File(
"10"
);
File filename2 =
new
File(
"shyam"
);
File filename3 =
new
File(
"s12"
);
File filename4 =
new
File(
"s12"
);
// Returns the HashCode
int
returnvalue1 = System.identityHashCode(filename1);
System.out.println(returnvalue1);
// Returns the HashCode
int
returnvalue2 = System.identityHashCode(filename2);
System.out.println(returnvalue2);
// Returns different HashCode for same filename
int
returnvalue3 = System.identityHashCode(filename3);
System.out.println(returnvalue3);
// Returns different HashCode for same filename
int
returnvalue4 = System.identityHashCode(filename4);
System.out.println(returnvalue4);
}
}
输出:589431969 1252169911 2101973421 685325104
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END