-4

Why is this False? I can't get it. DataBaseHelper.getBoolean(); is a string value. In this if statement it outputs the same result but it says it is not equal with each other..

            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");
            }

getBoolean method from toher class.

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;
    }
user1816780
  • 121
  • 4
  • 14

3 Answers3

2

because String a and String b objects are different (== test for reference equality). you should compare String instance through equals

if (a.equals(b)) {}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

You should use a.equals(b) or a.equalsIgnoreCase(b) instead of == operator to compare String. == operator compares the references of the two operands. Your a and b are different String objects hence it fails .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

Try with one of following

if (a.equals(b)) {}

And

if (a.equalsIgnoreCase(b)) {}

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113