Identical strings comparison gives me false

12,300

Solution 1

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]);

Solution 2

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]);

Solution 3

== 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).

Share:
12,300
Hubro
Author by

Hubro

Code enthusiast!

Updated on July 24, 2022

Comments

  • Hubro
    Hubro almost 2 years

    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?

  • Erick Robertson
    Erick Robertson over 13 years
    -1 for missing the problem completely
  • Steve Kuo
    Steve Kuo over 13 years
    There's no need to use Boolean instead of boolean.
  • jdmichal
    jdmichal over 13 years
    Was just stating it as found in the OP. If it were mine, I would have used final boolean.
  • DJClayworth
    DJClayworth over 13 years
    Probably because using Boolean.booleanValue is much more complicated than boolean.
  • David
    David over 13 years
    and it would unboxed to the primitive boolean value anyway
  • Shervin Asgari
    Shervin Asgari over 13 years
    I am not the one who used Boolean. I just used the same code as the OP, but improved it
  • Shervin Asgari
    Shervin Asgari over 13 years
    @David: (un)Boxing is very expensive
  • David
    David over 13 years
    which is why he should use boolean. But you don't gain anything (except longer code) from calling booleanValue as that is what the Java 5+ compiler inserts for you anyway. The overhead comes from using reference types instead of primitives.
  • DJClayworth
    DJClayworth over 13 years
    I didn't say it was a good downvote, I just explained it.