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.
In order to compare two strings, we have to use a method called “equals”. Type the following into the parentheses of your If Statement: car1. equals() . In the parentheses of THIS code, write car2 as a parameter.
To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .
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
== 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With