Java对象类int hashCode()方法(带示例)
对象类inthashCode()
包java.lang.Object.hashCode()中提供了此方法。
此方法用于返回对象的哈希码。
语法:
int hashCode(){
}参数:
我们不会在Object方法中将任何对象作为参数传递。
返回值:
此方法的返回类型为int,表示此方法返回对象的哈希码,返回类型为int,表示哈希码为数字格式。
Java程序演示对象类hashCode()方法的示例
import java.lang.Object;
public class ObjectClass {
public static void main(String[] args) {
//为Integer类型创建一个新对象
Integer in = new Integer(10);
//显示Integer类对象的哈希码
System.out.println("The hashCode of Integer class object in is :" + in .hashCode());
//为String类型创建一个新对象
String str = new String("Hello, Welcome in java world");
//显示String类对象的哈希码
System.out.println("The hashCode of String class object str is :" + str.hashCode());
}
}输出结果
D:\Programs>javac ObjectClass.java D:\Programs>java ObjectClass The hashCode of Integer class object in is :10 The hashCode of String class object str is :508604331