9

Possible Duplicate:
Java String.equals versus ==

I'm trying to write a method in az DBOpenHelper extends SQLOpenHelper class. It supposed to evaluate if there's an entry in the DB with the same name.

public boolean existsContact(Contact contact) {

    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {

            String name = cursor.getString(1);
            String cname = contact.getName();
            if (name == cname) {

                cursor.close();
                db.close();
                return true;
            }

        } while (cursor.moveToNext());
    }

    db.close();

    return false;
}

Here's the relevant part of Contact class:

 public class Contact {

    String _name;
    public String getName(){
        return this._name;
    }
    }

Now here's the strange thing:

Scenario A : if (name == cname) where name = "foo" and cname = "foo" equals false. Eclipse debugger show name's foo and cname's foo have different id's. both variables filled as seen before in code.

Scenario B: if(name == cname) where variabales are loaded like this:

String name = "foo";
String cname = "foo";
         statement equals true as it's supposed to.

Scenario C: if("foo" == "foo") equals true...BUT...debugger goes out the window. LogCat show debugger connected, but there's no activity in eclipse's Debug perspective. Breakpoints have no effect. No Threads shown.

Community
  • 1
  • 1
Losó Adam
  • 528
  • 1
  • 5
  • 8
  • better you use name.equals(cname); Don't ever use == for string comparison. – Smit Dec 19 '12 at 02:48
  • you can change code to check if name exists using sql and avoid looping data Ex: `SELECT * FROM " + TABLE_CONTACTS + ' Where name = ' + contact.getName()` – rs. Dec 19 '12 at 02:49

3 Answers3

40

In java, when using == on two objects, you're not actually comparing the strings themselves. You'll need to use .equals(String).

== actually compares the two object's references, not their values.

string1.equals(String target) compares the two strings based off of the actual characters in the strings.

See: http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html

Michael
  • 3,334
  • 20
  • 27
8

== operator check object reference are equal or not but equals() method check values are same or not

    if (name == cname) 
   {    
    cursor.close();
    db.close();
    return true;
  }

change with it

 if (name.equals(cname)){        
    cursor.close();
    db.close();
   return true;
}
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
2

When comparing objects in java that are not primitive data types (int, char, boolean, etc...), you have to use the method Object#equals(Object), which returns a boolean.

So, when you are comparing two Strings, you are actually checking if the two Objects are the same instance, instead of the actual value.

All you have to do is just change name == cname to name.equals(cname).