Brent
Brent

Reputation: 83

Checking For Equal Instances of 2 Different (Included Example)

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!");
        }
    }
}

Upvotes: 8

Views: 720

Answers (6)

Arup Chowdhary
Arup Chowdhary

Reputation: 99

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.

Upvotes: 0

Brijesh
Brijesh

Reputation: 301

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.

Upvotes: 0

jjnguy
jjnguy

Reputation: 138922

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.

Upvotes: 10

codaddict
codaddict

Reputation: 455282

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.

Upvotes: 2

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

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.

Upvotes: 2

vodkhang
vodkhang

Reputation: 18741

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

Upvotes: 1

Related Questions