12

I have two identical strings, one in an array and one in a String variable. When I compare these IDENTICAL strings I get false every time. I have debugged and debugged, but I get the same result every time. Here is the code in question

String temp = ""+(num1*num2);
Boolean equal = temp == answers[i];

if(equal) {
    correct[i] = true;
    num_correct ++;
}else{
    correct[i] = false;
}

Again, I have debugged every minor detail of this program and I am 101% sure that the strings are IDENTICAL. Why is Java returning false on comparison?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • 2
    Why are you using a Boolean (java class) instead of the native boolean type ? Also, why using the == operator instead of the equals() method ? – SirDarius Oct 06 '10 at 15:57
  • 1
    We need a top ten FAQ list, == instead of equals comes up all the time. – Steve Kuo Oct 06 '10 at 16:27
  • @Steve Kuo That's a great idea. Should suggest it on meta if it's not there already. – jdmichal Oct 06 '10 at 17:15
  • 1
    Why the hell would you -1 me! == works on strings in every single programming language I've encountered. The possibility that it wouldn't work didn't even occur to me, nor does it to lots of other programmers. – Hubro Oct 08 '10 at 03:25

5 Answers5

25

When you use the == operator in Java with objects, you are attempting to compare object references. That is, is this object handle pointing to the EXACT same object as this other object handle. Unless the strings are interned, this will not work.

Use String.equals(Object) instead:

Boolean equal = temp.equals(answers[i]);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jdmichal
  • 10,984
  • 4
  • 43
  • 42
5

You are doing reference comparison, not value comparison. When you use the == operator its checking to see if the references are equal, and they aren't. If you want to check whether the values are equal use the equals method.

boolean equal = temp.equals(answers[i]);
Corey Sunwold
  • 10,194
  • 6
  • 51
  • 55
1

== in java for strings is comparing to see if they are the same object, not the same string value. You should use .equals instead which will compare the value. == works sometimes because the strings can be interned and refer to the same object via reference even if created seperately through the same literal (so string b = "Hey" and string c = "Hey" end up being the same object in the background because "Hey" got interned to a hidden string object).

Mark
  • 983
  • 2
  • 10
  • 20
-1

As others have shown you should use equals.

But I would also use the booleanValue of the Boolean object.

Here is your code correctly done

String temp = ""+(num1*num2);
Boolean equal = temp.equals(answers[i]);

if(equal.booleanValue()) {
    correct[i] = true;
    num_correct ++;
}else{
    correct[i] = false;
}
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
-5

Does this help?

Boolean equal = (temp == answers[i]);

I'm not sure that would be an issue, but I always enclose my conditions in parenthesis.

Dutchie432
  • 28,798
  • 20
  • 92
  • 109