2

Possible Duplicate:
Java string comparison?

I was trying to do this:

boolean exit = false;
while(exit==false && convStoreIndex<convStoreLength) {
  if(conversionStore[convStoreIndex].getInUnit()==inUnit) {
    indexCount++;
    exit=true;
  }
  convStoreIndex++;
}

but the if-condition never went true, even if the two Strings were the same(checked this in the debugger). so I added some lines:

boolean exit = false;
while(exit==false && convStoreIndex<convStoreLength) {
  Log.v("conversionStore["+String.valueOf(convStoreIndex)+"]", conversionStore[convStoreIndex].getInUnit()+"|"+inUnit);
  String cs = conversionStore[convStoreIndex].getInUnit();
  String iu = inUnit;
  Log.v("cs", cs);
  Log.v("iu", iu);
  Log.v("Ergebnis(cs==iu)", String.valueOf(cs==iu));
  if(conversionStore[convStoreIndex].getInUnit()==inUnit) {
    indexCount++;
    exit=true;
  }
  convStoreIndex++;
}

and here is the extract from LogCat:

09-15 11:07:14.525: VERBOSE/cs(585): kg
09-15 11:07:16.148: VERBOSE/iu(585): kg
09-15 11:07:17.687: VERBOSE/Ergebnis(cs==iu)(585): false

the class of conversionStore:

class ConversionStore {
  private String inUnit;
  [...]
  public String getInUnit() {
    return inUnit;
  }
}

Who is going crazy, java or me?

Community
  • 1
  • 1

6 Answers6

13

Don't use == to compare String objects, use .equals():

if(conversionStore[convStoreIndex].getInUnit().equals(inUnit)) {
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 2
    I'm actually quite glad that I was already over my 200-point cap today, because getting 140 rep for this answer would be just wrong. Especially since it's a clear duplicate. – Joachim Sauer Sep 15 '11 at 12:15
6

To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality.

Straight from the first link Google provided when searching "Java string comparison"...

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

Please use String.equals() to compare by string content instead of reference identity.

b_erb
  • 20,932
  • 8
  • 55
  • 64
3

You are comparing your Strings using == : cs==iu.

But this will return true only if both Strings are actually the same object. This is not the case here: you have two distinct instances of String that contain the same value.

You should use String.compareTo(String).

Shlublu
  • 10,917
  • 4
  • 51
  • 70
3

use .equals(); method instread of ==

Becase ::

They both differ very much in their significance. equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.

For example, lets say, you have two String objects and they are being pointed by two different reference variables s1 and s2.

 s1 = new String("abc");
 s2 = new String("abc");

Now, if you use the "equals()" method to check for their equivalence as

if(s1.equals(s2))
  System.out.println("s1.equals(s2) is TRUE");
else
  System.out.println("s1.equals(s2) is FALSE");

You will get the output as TRUE as the 'equals()' method check for the content equivality.

Lets check the '==' operator..

if(s1==s2)
 System.out.printlln("s1==s2 is TRUE");

else System.out.println("s1==s2 is FALSE");

Now you will get the FALSE as output because both s1 and s2 are pointing to two different objects even though both of them share the same string content. It is because of 'new String()' everytime a new object is created.

Try running the program without 'new String' and just with

String s1 = "abc";
String s2 = "abc";  

You will get TRUE for both the tests.

Reson::

After the execution of String str1 = “Hello World!”; the JVM adds the string “Hello World!” to the string pool and on next line of the code, it encounters String str2 =”Hello World!”; in this case the JVM already knows that this string is already there in pool, so it does not create a new string. So both str1 and str2 points to the same string means they have same references. However, str3 is created at runtime so it refers to different string.

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
2

Two things: You don't need to write "boolean==false" you can just write "!boolean" so in your example that would be:

while(!exit && convStoreIndex<convStoreLength) { 

Second thing: to compare two Strings use the String.equals(String x) method, so that would be:

if(conversionStore[convStoreIndex].getInUnit().equals(inUnit)) { 
Matschie
  • 1,237
  • 10
  • 9