返回列表 發帖
  1. public class MyClass_test
  2. {
  3.         public static void main(String args[])
  4.         {
  5.                 String str1 = new String("hello");  
  6.                 String str2 = new String("hello");
  7.                
  8.                 if(str1==str2)
  9.                 {
  10.                         System.out.println("str1==str2");
  11.                 }
  12.                 else
  13.                 {
  14.                         System.out.println("str1!=str2");
  15.                 }
  16.                
  17.                 if(str1.equals(str2))
  18.                 {
  19.                         System.out.println("str1 equal str2");
  20.                 }
  21.                 else
  22.                 {
  23.                         System.out.println("str1 not equal str2");
  24.                 }
  25.                
  26.                 System.out.println("str1 =" + str1);
  27.                 System.out.println("str2 =" + str2);               
  28.         }
  29. }
複製代碼
  1. 執行結果
  2. str1!=str2
  3. str1 equal str2
  4. str1 =hello
  5. str2 =hello
複製代碼
"=="為比較是否為同一物件,若指向同一物件則傳回true
"equals"比較了2個物件的屬性(?),若相同則傳回true
那若是基本型態的比較(非物件)是否"=="和"equals"都會傳回一樣結果 是這樣嗎??

TOP

返回列表