Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement comparing strings in java android [duplicate]

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.

like image 593
Losó Adam Avatar asked Dec 19 '12 02:12

Losó Adam


People also ask

How can I compare two strings in Android?

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.

Can we compare 2 strings using == in Java?

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.

How do you compare two strings to see if they are the same?

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.

What happens when you use == to compare strings in Java?

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 .


2 Answers

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

like image 153
Michael Avatar answered Oct 20 '22 00:10

Michael


== 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;
}
like image 31
Mohammod Hossain Avatar answered Oct 19 '22 23:10

Mohammod Hossain