2013-06-18 70 views
-4

這是爲什麼是假?我無法得到它。 DataBaseHelper.getBoolean();是一個字符串值。在這個if語句中,它輸出相同的結果,但它表示它不相等。我沒有得到什麼。 String == String False

  String a = DataBaseHelper.getBoolean(id); 
      String b = DataBaseHelper.getBoolean(id); 
      if (a==b){ 
       newTextView.setText(DataBaseHelper.getBoolean(id) + " == " + DataBaseHelper.getBoolean(id) + " is TRUE \n"); 
      } else { 
       newTextView.setText(DataBaseHelper.getBoolean(id) + " == " + DataBaseHelper.getBoolean(id) + " is FALSE \n"); 
      } 

從toher類的getBoolean方法。

public String getBoolean(int randomIndex) { 
     // TODO Auto-generated method stub 
     open(); 
     Cursor c = myDataBase.query(TABLE_NAME1, columns, WHATTODONOW_COLUMN_ID 
       + "=" + randomIndex, null, null, null, null); 
     if (c != null) { 
      c.moveToFirst(); 
      String text = c.getString(8); 
      return text; 
     } 
     closee(); 
     return null; 
    } 
+1

使用'=='比較字符串值將不起作用,請使用equals()方法。 – Geros

+0

嘗試先搜索你的問題,然後問:) –

回答

2

因爲字符串a和字符串b對象是不同的(==測試引用相等)。你應該通過equals

if (a.equals(b)) {} 
+0

奇怪。其實這不是「地址」。這是因爲'a'和'b'都是不同的客體。 –

+0

這就是我的意思。您的定義更清晰,謝謝@PankajKumar – Blackbelt

+0

沒有Java地址的概念。 BTW最受歡迎 –

1

比較String對象應使用a.equals(b)a.equalsIgnoreCase(b),而不是==符來比較字符串。 ==運算符比較兩個操作數的引用。您的ab是不同的字符串對象,因此它失敗。

1

與一種嘗試以下

如果(a.equals(B)){}

而且

如果(a.equalsIgnoreCase(B)){}

相關問題