hashCode()方法
-
public int hashCode(){}
-
返回该对象的哈希码值。
-
哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值。
-
一般情况下相同对象返回相同哈希码。
-
案例
Student类
public class Student { //定义两个属性 private String name; private int age; //定义 get set 方法 和有参无参构造 public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } }
main方法
public class TestClass { public static void main(String[] args) { Student s1 = new Student("李XX",18); Student s2 = new Student("王XX",19); //hashCode()方法 System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); //输出 460141958 1163157884 } }