smasher smasher - 2 years ago 69
Java Question

java "== " unusual behaviour

import java.lang.String;
pubic class Test {
public static void main(String[] args) {
String a1="ka";
String a2="ka";
System.out.println("a1==a2? "+(a1==a2));
String a3="k";
String a4=new String("k");
System.out.println("a3==a4? "+(a3==a4))
System.out.println("a3==a4? "+(a3==a4.intern()));
String a5="k";
String a6=a4+"a";
System.out.println("a1==a6? "+(a1==a6));


Output that i got:

a1==a2? true
a3==a4? false
a3==a4? true
a1==a6? false

  • a1===a2 is true as line 5 will not create new String literal in string pool area.Only reference to previously created string is returned.

  • a3==a4? false as a4 will have refernce to the String object instead of the string in the string in string pool area. My question is if a3 is referencing the string constant instead of the String object, how is it able to use the methods of the String class?

  • a4.intern() will return the reference to the string in the string pool which happens to be same as a3

  • a6=a4+"a" will create a new string "ka". But this actually make use of StringBuilder class and its append method . It is then converted to string using toString(). Does this process store the newly created string "ka" in the string pool area? Since the string is already in the pool the code at line 12 should return the reference to it. So the a1==a6 should be true.rt?
    I am new to java . Please guide me where i am doing the mistake?

Answer Source

You are comparing the Strings wrongly (because you are in fact comparing references)

String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.

String is immutable and final in Java and in this case JVM uses String Pool to store all the String objects.

What are different ways to create String Object?

We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.

To your Question:

When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.

When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

So when you do

String s1 = "abc";
String s2 = "abc";

those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.

but when you do:

String s3 = new String("abc");
String s4 = new String("abc"); 

you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.

Please take a look at the image below for a more illustrative example.

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download