package test;
public class TestEqals {
public static void main(String args[]){
OverWrite first=new OverWrite(6);
OverWrite second=new OverWrite(6);
System.out.println(first.equals(second));
System.out.println(first==second);
NoOverWrite three=new NoOverWrite(6);
NoOverWrite four=new NoOverWrite(6);
System.out.println(three.equals(four));
System.out.println(three==four);
}
}
class OverWrite{
private int number;
OverWrite(int i){
this.number=i;
}
public int hashCode(){
return this.number;
}
public boolean equals(Object o){
if(this.hashCode()==o.hashCode())
return true;
else{
return true;
}
}
}
class NoOverWrite{
private int number;
NoOverWrite(int i){
this.number=i;
}
}
/**
*1.如果不重写hashCode和equal,那么用new运算符,新建的对象都会是不同的对象(String对象除外,因为他已经默认重写了)。
*2.如果不重写hashCode,则hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值 public int hashCode()返回该对象的哈希码值。
*3.HashMap对象是根据其Key的hashCode来获取对应的Value。