java中equals與==的用法比較,java中equals用法

來源:互聯網
上載者:User

java中equals與==的用法比較,java中equals用法
1.equals用法

個人的歸納總結:

  • 用於String類型之間的比較時,調用的是String類的equals()方法,比較的是字串的內容而不是地址,並且只能用於比較String類型,因為StringBuffer和StringBuilder類中都沒有equals()方法。
  • 用於其他類型之間(包括含有一個Sting類型的情況)的比較時,以o1.equals(o2)為例: 若o1是String類型,則調用的時String類的equals方法,傳回值為false;若o1不是String類型,則調用的是Object類的equals()方法(此時與==用法相同),比較的是引用變數的地址而不是內容。
 1     private static void equalsAnal() { 2         String s1 = "hello"; 3         String s2 = "hello"; 4         String s3 = new String("hello"); 5         String s4 = new String("hello"); 6         String s5 = new String(new StringBuffer("hello")); 7          8         StringBuffer s6 = new StringBuffer("hello"); 9         StringBuffer s7 = new StringBuffer("hello");10         11         System.out.println(s1.equals(s2));//true12         System.out.println(s3.equals(s4));//true13         14         System.out.println(s1.equals(s3));//true15         16         System.out.println(s1.equals(s5));//true17         System.out.println(s3.equals(s5));//true18         19         System.out.println(s6.equals(s7));//false20         System.out.println(s1.equals(s6));//false21         System.out.println(s3.equals(s6));//false22         System.out.println(s5.equals(s6));//false23         System.out.println(s6.equals(s1));//false24     }

對於上述代碼的結果,我們可以結合String類以及Object類的源碼來進行分析:

 1   //String類中的equals方法   2   public boolean equals(Object anObject) { 3         if (this == anObject) { 4             return true; 5         } 6         if (anObject instanceof String) { 7             String anotherString = (String)anObject; 8             int n = value.length; 9             if (n == anotherString.value.length) {10                 char v1[] = value;11                 char v2[] = anotherString.value;12                 int i = 0;13                 while (n-- != 0) {14                     if (v1[i] != v2[i])15                         return false;16                     i++;17                 }18                 return true;19             }20         }21         return false;22     }
1     //Object類中的equals方法2      public boolean equals(Object obj) {3         return (this == obj);4     }
 2. ==用法

 ==是一種關係運算子,用於基礎資料型別 (Elementary Data Type)時,比較的是兩個變數的值是否相等;用於參考型別時,比較的是兩個引用變數的地址是否相同。

 1         int a = 10, b = 10; 2         char ch1 = 'A', ch2 = 'A'; 3         String str1 = "hello"; 4         String str2 = "hello"; 5         Integer n1 = new Integer(10); 6         Integer n2 = new Integer(10); 7          8         System.out.println(a == b);//true 9         System.out.println(ch1 == ch2);//true10         System.out.println(str1 == str2);//true11         System.out.println(n1 == n2);//false
3.用法比較
 1         String s1 = "hello"; 2         String s2 = "hello"; 3         String s3 = new String("hello"); 4         String s4 = new String("hello"); 5         String s5 = new String(new StringBuffer("hello")); 6          7         StringBuffer s6 = new StringBuffer("hello"); 8         StringBuffer s7 = new StringBuffer("hello"); 9         10         System.out.println(s1.equals(s2));//true11         System.out.println(s1==(s2));//true12         13         System.out.println(s3.equals(s4));//true14         System.out.println(s3==(s4));//false15         16         System.out.println(s1.equals(s3));//true17         System.out.println(s1==(s3));//false        18         19         System.out.println(s1.equals(s5));//true20         System.out.println(s1==(s5));//false21         22         System.out.println(s3.equals(s5));//true23         System.out.println(s3==(s5));//false24         25         System.out.println(s6.equals(s7));//false26         System.out.println(s1.equals(s6));//false27         System.out.println(s3.equals(s6));//false28         System.out.println(s5.equals(s6));//false29         System.out.println(s6.equals(s1));//false

 綜上所述,equals和==用法相同的情況:

  • 字串為基礎資料型別 (Elementary Data Type)String的變數;
  • 比較的是兩個非String類型的引用變數。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.