views:

97

answers:

6

I use the == in the code below and prints out "Equals!", why? Can someone explain why these two different strings a and b are equal?

public class test
{
    public static void main()
    {
        String a = "boy";
        String b = "boy";

        if(a == b)
        {
            System.out.println("Equals!");
        }
        else
        {
            System.out.println("Does not equal!");
        }
    }
}
+2  A: 

This article will explain it in details:

What is the difference between == and equals() in Java?

After the execution of String a = “boy”; the JVM adds the string “boy” to the string pool and on the next line of the code, it encounters String b = ”boy” again; in this case the JVM already knows that this string is already there in the pool, so it does not create a new string. So both strings a and b point to the same string what means they point to the same reference.

Leniel Macaferi
== compares object references in Java, and not content.
Hippo
Hippo - I overlooked this one. Just corrected my mistake with a good reference.
Leniel Macaferi
ok, just cancel my vote down
vodkhang
@Leniel, cool - I've cancelled my down-vote.
Hippo
+1  A: 

Because the run time will have a string pool and when you need to assign a new constant string, the run time look inside the pool, if the pool contains it, then they set the variable point to the same String object inside the pool.

But you should never depends on this to check for content string equals. You should use the method: equals

vodkhang
+10  A: 

This is due to String interning.

Java (The JVM) keeps a collection of String literals that is uses to save memory. So, whenever you create a String like so:

String s = "String";

Java 'interns' the string. However, if you create the String like so:

String s = new String("String");

Java will not automatically intern the String. If you created your strings this way, your code would produce different results.

A quick Google search reveals lots of good resources regarding String interning.

jjnguy
+2  A: 

String a = "boy"; will create a new string object with value ("boy"), place it in the string pool and make a refer to it.

When the interpreter sees String b = "boy";, it first checks to see if string "boy" is present in the string pool, since it is present, no new object is created and b is made to refer to the same object that a is referring to.

Since both references contain the same content they pass the equality test.

codaddict
A: 

As rightly explained above, in the case of '==' comparison, the runtime will look into the String pool for the existence of the string. However, it very much possible that during garbage collection, or during memory issues, the virtual machine might destroy the string pool. The "==" operator therefor might or might not return the correct value.

Lesson - Always use equals() for comparison.

Arup Chowdhary
This, in my opinion, is one of the big weaknesses of the Java language. They should have just made it so that the == and != operators work with strings the way that normal people expect them to.
Mike Baranczak
No, the runtime will *not* look in the string pool when you apply the `==` operator. `==` is just a pointer comparison.
finnw
A: 

Whenever we create a string like below :

String str1 = "abc";
String str2 = "abc";

JVM will check the str2 = "abc" in the string constant pool, if it is present then it wont create a new String instead it point to the string one in the string constant pool.

But in case of this String str = new String("abc"); it will always create a new String Object but we can use intern() function to force JVM to look into the string constant pool.

Brijesh