String comparison with logical operator in Java

39,025

Solution 1

You shouldn't use == because it does something else then you think.

In this case, the "hello" is saved (Read up on string interning), so it is "coincidence" that it is the same thing as your stirng.

== checks if two things are EXACTLY the same thing, not if they have the same content. This is a really big difference, and some accidental (though explainable) "false possitives" are no reason to use this method.

Just use equals for string comparison.

From this site an example: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

String a = new String ("a");
String b = new String ("a");
System.out.println (a == b);

It returns false, while the following code returns true.

String a = new String ("a");
String b = new String ("a");
System.out.println (a.equals(b));

Solution 2

The magic is called interning.

Java interns String literals and so there is a high probability that it evaluates to true:

String a = "hello";       // "hello" is assigned *and* interned
String b = "hello";       // b gets a reference to the interned literal
if (a == b)
   System.out.println("internd.");

String c = "hello" + Math.abs(1.0);   // result String is not interned
String d = "hello" + Math.abs(1.0);   // result String is not interned
System.out.println(c==d);             // prints "false"

c = c.intern();
System.out.println(c==d);             // prints "false"

d = d.intern();
System.out.println(c==d);             // prints "true"

Solution 3

The == operator is used to check if both are references to the same String object while .equals compares the values.

Solution 4

It works in your case (and AFAIK it has been working like that in earlier JVMs as well) because s refers to the string literal "hello". String references initialized with literals refer to the literal (which is a global object in the background), not a separately created new object. The term for this, as others have mentioned too, is interning. Thus s and "hello" in your example refer to the same physical object. Consider

String s1 = "hello";
String s2 = "hello";
String s3 = "hello";

All of these strings refer to the same physical object, thus '==' comparison between any pair of these, or between any of them and "hello" returns true.

However

String s4 = new String ("hello");

creates a new object, thus s4 == "hello" yields false.

Share:
39,025
Sundeep
Author by

Sundeep

Updated on December 29, 2020

Comments

  • Sundeep
    Sundeep over 3 years

    When comparing two strings, I was taught that we shouldn't use the logical operator (==). We should use String.equals(String) for the comparison. However, I see that the following code complies and prints "Hello Friend" with the latest JDK(1.6_23). I tried searching around and couldn't find any reference. From when is this happening?

    public class StringComp{
    public static void main(String args[]){
            String s = "hello";
            if(s=="hello"){
                    System.out.println("Hello Friend");
            }else{
                    System.out.println("No Hello");
            }
        }
    }