Search code examples
androidif-statementequals-operator

I'm not getting something. String == String False


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

Solution

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

    if (a.equals(b)) {}