equals和==的區別

來源:互聯網
上載者:User

標籤:

package day07;public class Test1227 {    public static void main(String[] args)  {        String a = "hello2";         final String b = "hello";        String d = "hello";        String f = "hello";        String c = b + 2;         String e = d + 2;        System.out.println(a+"   a");        System.out.println(b+"   final b");        System.out.println(c+"   b+2 ");        System.out.println(d+"   d");        System.out.println(e+"   d+2");        System.out.println((a == c));        System.out.println((a == e));        System.out.println(a.equals(e));        System.out.println("\n\n\n");        System.out.println(d==f);        System.out.println(d.equals(f));        String ff = new String("hello");        System.out.println("\n\n");        System.out.println(f.equals(ff));        System.out.println(f==ff);    }}

hello2 a
hello final b
hello2 b+2
hello d
hello2 d+2
true
false
true


 



true
true


 


true
false

 

 

 java中equals和==的區別 實值型別是儲存在記憶體中的堆棧(簡稱棧),而參考型別的變數在棧中僅僅是儲存參考型別變數的地址,而其本身則儲存在堆中。 
==操作比較的是兩個變數的值是否相等,對於引用型變數表示的是兩個變數在堆中儲存的地址是否相同,即棧中的內容是否相同。 
equals動作表示的兩個變數是否是對同一個對象的引用,即堆中的內容是否相同。 

==比較的是2個對象的地址,而equals比較的是2個對象的內容。 
顯然,當equals為true時,==不一定為true; 
一、String中的equals和== 
Java代碼  
  1. 1、   
  2. public class TestString {   
  3.      public static void main(String[] args) {   
  4.          String s1 = "Monday";   
  5.          String s2 = "Monday";   
  6.      }   
  7. }   
上面這段程式中,到底有幾個對象呢? 
來檢測一下吧,稍微改動一下程式 
Java代碼  
  1. public class TestString {   
  2.      public static void main(String[] args) {   
  3.          String s1 = "Monday";   
  4.          String s2 = "Monday";   
  5.          if (s1 == s2)   
  6.              System.out.println("s1 == s2");   
  7.          else   
  8.              System.out.println("s1 != s2");   
  9.      }   
  10. }   
編譯並運行程式,輸出:s1 == s2 
說明:s1 與 s2 引用同一個 String 對象 -- "Monday"! 

2. 
再稍微改動一下程式,會有更奇怪的發現: 
Java代碼  
  1. public class TestString {   
  2.      public static void main(String[] args) {   
  3.          String s1 = "Monday";   
  4.          String s2 = new String("Monday");   
  5.          if (s1 == s2)   
  6.              System.out.println("s1 == s2");   
  7.          else   
  8.              System.out.println("s1 != s2");   
  9.          if (s1.equals(s2))   
  10.              System.out.println("s1 equals s2");   
  11.          else   
  12.              System.out.println("s1 not equals s2");   
  13.      }   
  14. }   
我們將 s2 用 new 操作符建立 
程式輸出: 
s1 != s2 
s1 equals s2 
說明:s1 s2分別引用了兩個"Monday"String對象 

3. 字串緩衝池 

原來,程式在啟動並執行時候會建立一個字串緩衝池 
當使用 s2 = "Monday" 這樣的表達是建立字串的時候,程式首先會 
在這個String緩衝池中尋找相同值的對象,在第一個程式中,s1先被 
放到了池中,所以在s2被建立的時候,程式找到了具有相同值的 s1 
將 s2 引用 s1 所引用的對象"Monday" 
第二段程式中,使用了 new 操作符,他明白的告訴程式: 
"我要一個新的!不要舊的!"於是一個新的"Monday"Sting對象被創 
建在記憶體中。他們的值相同,但是位置不同,一個在池中遊泳 
一個在岸邊休息。哎呀,真是資源浪費,明明是一樣的非要分開做什麼呢? 
4. 
再次更改程式: 
Java代碼  
  1. public class TestString {   
  2.      public static void main(String[] args) {   
  3.          String s1 = "Monday";   
  4.          String s2 = new String("Monday");   
  5.          s2 = s2.intern();   
  6.          if (s1 == s2)   
  7.              System.out.println("s1 == s2");   
  8.          else   
  9.              System.out.println("s1 != s2");   
  10.          if (s1.equals(s2))   
  11.              System.out.println("s1 equals s2");   
  12.          else   
  13.              System.out.println("s1 not equals s2");   
  14.      }   
  15. }   
這次加入:s2 = s2.intern(); 
程式輸出: 
s1 == s2 
s1 equals s2 

原來,(java.lang.String的intern()方法 
"abc".intern()方法的傳回值還是字串"abc",表面上看起來好像這個方法沒什麼用處。但實際上,它做了個小動作: 
檢查字串池裡是否存在"abc"這麼一個字串,如果存在,就返回池裡的字串;如果不存在,該方法會把"abc"添加到字串池中,然後再返回它的引用。 
) 
更好的辦法: 
把所有的String都intern()到緩衝池去吧 
最好在用到new的時候就進行這個操作 
String s2 = new String("Monday").intern(); 
然後就可以用==比較兩個字串的值了 


二、單一資料型別和封裝類中的equals和== 
Java為每一個單一資料型別提供了一個封裝類,每個基礎資料型別 (Elementary Data Type)可以封裝成物件類型。 
除int(Integer)和char(Character),其餘類型首字母大寫即成封裝類類型名。double (Double), float(Float),long(Long), short(Short),byte(Byte),boolean(Boolean). 
  
以int和Integer為例說明 
Java中int和Integer區別如下: 
1.int是基本的資料類型,預設值可以為0; 
2.Integer是int的封裝類,預設值為null; 
3.int和Integer都可以表示某一個數值; 
4.int和Integer不能夠互用,因為他們兩種不同的資料類型; 
int a1=1; 
int a2=1; 
Integer b1 =new Integer (1); 
Integer b2 =new Integer (1); 
------------------------------ 
a1==a2 這個是成立的,很簡單,都知道 
a1==b1 這個是不成立的.運算式的值為 false ,它們是不同的資料類型(在jdk1.5以上版本中為true) 
b1==b2 這個也是不成立的.運算式的值為 false,雖然是相同的資料類型,但是它們是兩個對象,==比較的是2個對象的地址,它們的地址是不相等的,內容相等都是1; 
b1.equals(b2)==true 這個是成立的,運算式的值為 true. 相同資料類型,兩個對象,地址不同,內容相同, quals比較的是2個對象的內容,所以成立。 
(a.equals(b),因為equals比較的是兩個對象,所以a,b都不能為基礎資料型別 (Elementary Data Type),否則會出編譯錯誤。)(在jdk1.5以上版本中,b可以為基礎資料型別 (Elementary Data Type),a不可以) 
同理,其它的封裝類和基本類型也是這樣的. 
java中equals和==的區別 
==比較的是2個對象的地址,而equals比較的是2個對象的內容。 
在jdk1.5以上的版本中,基本類型和封裝類能自動轉化,與String類型的對象和字串常量類似。 
      Java代碼  
  1. Integer i1 = 123;   
  2.        Integer i2 = 123;   
  3.          
  4.        int i = 123;   
  5.          
  6.        Integer i3 = new Integer(123);   
  7.        Integer i4 = new Integer(123);          
  8.                 
  9.        System.out.println("i1 == i2 = "+(i1 == i2));   
  10.        System.out.println("i1.equals(i2) = "+(i1.equals(i2)));   
  11.          
  12.        System.out.println();   
  13.        System.out.println("i3 == i4 = "+(i3 == i4));   
  14.        System.out.println("i3.equals(i4) = "+(i3.equals(i4)));   
  15.          
  16.        System.out.println();   
  17.        System.out.println("i2 == i4 = "+(i2 == i4));   
  18.        System.out.println("i2.equals(i4) = "+(i2.equals(i4)));   
  19.          
  20.        System.out.println();   
  21.        System.out.println("i == i2 = "+(i == i2));   
  22.        System.out.println("i1.equals(i) = "+(i1.equals(i)));   
  23.          
  24.        System.out.println();   
  25.        System.out.println("i == i4 = "+(i == i4));   
  26.        System.out.println("i4.equals(i) = "+(i4.equals(i)));   
  27.        ------------------------------   
  28.       i1 == i2 = true   
  29.      i1.equals(i2) = true   
  30.      i3 == i4 = false   
  31.      i3.equals(i4) = true   
  32.      i2 == i4 = false   
  33.      i2.equals(i4) = true   
  34.      i == i2 = true   
  35.      i1.equals(i) = true   
  36.      i == i4 = true   
  37.      i4.equals(i) = true   
三、其他類怎麼使用equals和== 
API裡的類大部分都重寫了equals方法,沒有重寫的一般是自己寫的類, 
如果是你自己定義的一個類,比較自訂類用equals和==是一樣的,都是比較控制代碼地址, 
因為自訂的類是繼承於object,而object中的equals就是用==來實現的,你可以看源碼。 

四、java裡equals和hashCode之間什麼關係 
只是為了維護 hashCode 方法的常規協定,才要求用equals比較的兩個對象的hashCode相同. 
equals()和hashCode()都來自java.lang.Object.你當然可以重寫. 

比如a.equals(b).僅當a的記憶體位址相等時,才返回true.當然如String等類已經對這個方法進行了重寫,比較的就不再是記憶體位址了. 
hashCode()的值也是與記憶體位址相關的.所以僅當記憶體位址相等時,hashCode才相等. 

同樣很多類也重寫了這個方法,還是以String為例: 
Java代碼  
  1. public int hashCode() {   
  2. int h = hash;   
  3. if (h == 0) {   
  4.       int off = offset;   
  5.       char val[] = value;   
  6.       int len = count;   
  7.   
  8.       for (int i = 0; i < len; i++) {   
  9.          h = 31*h + val[off++];   
  10.       }   
  11.       hash = h;   
  12.    }   
  13.    return h;   
  14. }   
就不在與記憶體位址相關了.這樣做是為了保證用equals比較返回為true的兩個對象,他們的hashCode是相同的. 

所以一般重寫equals的時候都會重寫hashCode(). 
當然,這個相當於一個約定,一個協議.你不這麼做並不會錯. 

五、hashCode 
在一般的應用中你不需要瞭解hashcode的用法,但當你用到hashmap,hashset等集合類時要注意下hashcode。 

你想通過一個object的key來拿hashmap的value,hashmap的工作方法是, 
通過你傳入的object的hashcode在記憶體中找地址, 
當找到這個地址後再通過equals方法來比較這個地址中的內容是否和你原來放進去的一樣,一樣就取出value。 

所以這裡要匹配2部分,hashcode和equals 
但假如說你new一個object作為key去拿value是永遠得不到結果的, 
因為每次new一個object,這個object的hashcode是永遠不同的,所以我們要重寫hashcode, 
你可以令你的hashcode是object中的一個恒量,這樣永遠可以通過你的object的hashcode來找到key的地址, 
然後你要重寫你的equals方法,使記憶體中的內容也相等。。。 

equals和==的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.