赣州新闻网

首页 / 科技

一道经典的Java面试题:equals,== 和hashcode()的区别

2020-01-12 · 赣州新闻网 · 科技

对于基本类型是值比较,对于引用类型来说是引用比较。

	/**
* == 的比较
*/
@Test
public void testOne(){
int a = 200;
int b = 200;
Integer c = 200;
Integer d = 200;
//值比较
SystI : A 8 Q x , )em.) W C I j - #out.println(a == b);//同基本类型同值比较:true
//引用类型比较
System.out.println(c == d);//false3 L J I = v , r
}

equals:

equals是原始类Object的方法,即所有对象都有equals方法,默认情况下(即没有重写| / w $ c { 8 / nse;
}

//String% E ( 7 ! # e D 7:
//先比较地址,然后判断是否为同一类型,不是直接false,是b e M的话在进行值比较
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof S} R l = , #tring) {
StZ s g z a B $ Brw q # g { t d U ing anotherS. a Q n ; Vtring = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1 = value;
char v2 = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1 != v2)
return false;
i++;
}
return true;
}
}
return false;
}

en c k |quals的比较

class Cat{
String2 D A b A name = \"cat\";
}
class Dog{
String name = \"dog\"! 7 q z N = [ p;
}
______________I 8 - r__________________________________________________________
/**
* euqals 比较
*/
@Test
public void testTwo(){
int a = 200;
int b = 200;
Integer c = 300;? r [
Integer d = 300;
Cat cat = new Cat();i ? u 9 ) R p q S
Dog dog = new Dog();
Syst3 J z y +em.out.println(c.equO _ Wls(d));//true
//System.out.pn j h S : &rintln(a.equals(cat));//基本类型不能使用equals比较,而是用==,因为基本类型没有equals方法
}

hashcode():

hashcode()也是Object的方法,是一个y o本地方法,用W b L 4 D # OC/r * 8 7C++语言实现的,由java去调用返回的对象的地址值。但JDK中很多类都对hashcode()进行了重写。比如Boolean的表示true则哈希值为1c ( E h { V G % r231,表示false则哈希值为123e 4 q r m ]7,

	//Obl k b gject:
public native int hashCode();

//Integer直接返回值
public int hal T ^ fshCo H g f o p Ude() v e s u d J D {
return Integer.hashCode(value);
}
public static in: A / Vt hashCode(int val4 u I due) {
return value;
}

//String 返回此字符串的哈希码。
public int hashCode() {
int h = hash;
if (h =s 0 ; # 9 W D X K= 0 && v ) ) #alue.length > 0) {
char val = value;

for (int i = 0; i < value.length; i++) {
h = 31 * h + val;
}
hash = h;
}
return h;
}

hashcode()的简单使用:

class Cat{
Str h O uring name = \"cat\";
}
class Dog{
String name = \"dog\";
}5 : ( T / a R
----------------------3 M * O T ; o Z o--------------------------------------------e W O 5 % s ;---
@Test
public void testThree(){
Cat cat = new Cat();
Dog dog = new Dog();
System.out.println(cat.hashCode());//204349222
System.out.println(dog.hashCode());//231685785
Integer a = 200;
IntegerV 5 Y 7 b = 300;
System.out.println(a.hashCode());//200
System.ouw C a q M ) 7t.println(b.hashCode4 & 8 d @ . ! } &());//300
}

equals 与 == ,hashg & O Q Q 3 . ~code()的比较:

  1. equal()相等的两个对象他们的hashCod$ : ~ U % [e()肯定相等。
  2. hashCode()相等的两个对象他们的equal()不一定相等。

可以尝试使用联想法去记住以上概念。e L } s比如我们使用的HashMap。其结构如下:

一道经典的Java面试题:equals,== 和hashcode()的区别

hashcode()相等,那么它们具有 相同的桶的位置,此时就如Entry1和E6 W 2ntry2,但是,Entry1和Entry2的equals并不一定想等,这是再! D x v Z E O -举个例子Entry1U ; / [ w b n=abc,Ent| S ) nry2=abc,那么它们是相等的,但是Entry1=abc,Entry2=def,那么它们是不相等的. equals相等,那么说明它们在同一列上,那意味着桶的位置一样,则.hashCode()肯定相同

认真写文章,用心做分享。公众号:Java耕耘者 文章都会在里面更新,整理的资料也会放在里面。

经典类型上犹区别重写方法引用类型判断引用