7

Possible Duplicate:
Java String.equals versus ==

I know it' a dumb question but why this code doesn't work.

boolean correct = "SampleText"  == ((EditText)findViewById(R.id.editText1)).getText().toString();
    if(correct) ((TextView)findViewById(R.id.textView1)).setText("correct!");
    else ((TextView)findViewById(R.id.textView1)).setText("uncorrect!");  

The point is to check if content of "editText1" is equal to "Sample Text"

Community
  • 1
  • 1
Pawelnr1
  • 243
  • 2
  • 5
  • 11

5 Answers5

26

In Java, two strings (and in general, two objects) must be compared using equals(), not ==. The == operator tests for identity (meaning: testing if two objects are exactly the same in memory), whereas the method equals() tests two objects for equality (meaning: testing if two objects have the same value), no matter if they're two different objects. Almost always you're interested in equality, not in identity.

To fix your code, do this:

String str = ((EditText)findViewById(R.id.editText1)).getText().toString();
boolean correct = "SampleText".equals(str);

Also notice that it's a good practice to put the string literal first in the call to equals(), in this way you're safe in case the second string is null, avoiding a possible NullPointerException.

Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 3
    *"Also notice that it's a good practice to put the string literal first in the call to `equals()`, in this way you're safe in case the second string is null, avoiding a possible `NullPointerException`."* In robust code, that's actually an argument **against** putting the literal first. You **want** the NPE if your logic fails to handle that situation prior to the comparison, most of the time, so you can fix the logic before release. – T.J. Crowder Jun 17 '12 at 16:39
  • @T.J.Crowder that's open to discussion. Some static-analysis checkers will generate a warning if you _don't_ compare strings as I suggested above. One could argue that comparing strings in this way handles a corner case better than explicitly testing at all places to see if a string is null – Óscar López Jun 17 '12 at 16:47
  • @T.J.Crowder for example, look at the `EqualsAvoidNull` check in [Checkstyle](http://checkstyle.sourceforge.net/config_coding.html) – Óscar López Jun 17 '12 at 16:50
  • 1
    @ Óscar: As you say, it's something people can disagree on. I **never** check with the literal first, because it reads poorly (to native English speakers and indeed native speakers of most other western languages) and because, again, I want the NPE if I've done something stupid. So far? Precisely *zero* production bugs related to it. Some nice bugs caught in testing, which I then was able to fix. But again, it's something reasonable people can, and do, disagree on. :-) – T.J. Crowder Jun 17 '12 at 16:50
1

In Java Strings have to be compared with their equals() method:

String foo = "foo";
String bar = "bar";
if (foo.equals(bar)) System.out.println("correct");
else System.out.println("incorrect");
nkr
  • 3,026
  • 7
  • 31
  • 39
1

to compare the values for two strings (for equality), you need to use equals, not == (or use equalsIgnoreCase if you do not care about case sensitivity).
Using equals will check the contents/values of the strings (as opposed to "==" which will only check if the two variables point to the same object - not the same value).

ali haider
  • 19,175
  • 17
  • 80
  • 149
1

The correct way to compare 2 objects in java is using equals() method of Object class And as String is an object in java, it should be compared in same way.

The correct way to compare a String is with,

s1.equals(s2)

So you can use this,

boolean correct = "SampleText".equals(((EditText)findViewById(R.id.editText1)).getText().toString());

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0
((TextView)findViewById(R.id.textView1)).setText("SampleTest".equals(((EditText)findViewById(R.id.editText1)).getText().toString()) ? "correct!" : "incorrect!");

It's a bit long and theres probably a better way you could do this. The .toString() feels weird!

Emily
  • 693
  • 5
  • 10