面試:請(qǐng)說一下equals和==的區(qū)別?

equals比較的是兩個(gè)對(duì)象值是否相等,如果沒有被重寫,比較的是對(duì)象的引用地址是否相同;
==用于比較基本數(shù)據(jù)類型的值是否相等,或比較兩個(gè)對(duì)象的引用地址是否相等;
String hello = new String("hello");String hello1 = new String("hello");System.out.println(hello.equals(hello1)); //重寫了了,?比較的是值,輸出結(jié)果為trueSystem.out.println(hello == hello1); //?比較的是引?用地址,輸出結(jié)果為false//?比較基本類型的值int age = 10;int age2 = 10;System.out.println(age == age2); //輸出為true
評(píng)論
圖片
表情
