Java.lang.Object.hashCode() 方法
描述
java.lang.Object.hashCode() 方法返回对象的哈希码值。 支持这种方法是为了便于哈希表,例如 java.util.Hashtable 提供的哈希表。
声明
以下是 java.lang.Object.hashCode() 方法的声明。
public int hashCode()
参数
NA
返回值
此方法返回此对象的哈希码值。
异常
NA
示例
下面的例子展示了 lang.Object.hashCode() 方法的使用。
package com.tutorialspoint;
import java.util.GregorianCalendar;
public class ObjectDemo {
public static void main(String[] args) {
// create a new ObjectDemo object
GregorianCalendar cal = new GregorianCalendar();
// print current time
System.out.println("" + cal.getTime());
// print a hashcode for cal
System.out.println("" + cal.hashCode());
// create a new Integer
Integer i = new Integer(5);
// print i
System.out.println("" + i);
// print hashcode for i
System.out.println("" + i.hashCode());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Sat Sep 22 00:35:34 EEST 2012 -458465658 5 5

