What makes reference comparison (==) work for some strings in Java?

JavaStringEquals

Java Problem Overview


I have following lines of codes to compare String. str1 not equal to str2, which is understandable since it compares object reference. But then why s1 is equal to s2?

String s1 = "abc";
String s2 = "abc";
	
String str1 = new String("abc");
String str2 = new String("abc");
	
if (s1==s2)
    System.out.println("s1==s2");			
else
    System.out.println("s1!=s2");
	
if (str1==str2)
    System.out.println("str1==str2");			
else
    System.out.println("str1!=str2");
	
if (s1==str1)
    System.out.println("str1==s1");			
else
    System.out.println("str1!=s1");

Output:

  s1==s2
  str1!=str2
  str1!=s1 

Java Solutions


Solution 1 - Java

The string constant pool will essentially cache all string literals so they're the same object underneath, which is why you see the output you do for s1==s2. It's essentially an optimisation in the VM to avoid creating a new string object each time a literal is declared, which could get very expensive very quickly! With your str1==str2 example, you're explicitly telling the VM to create new string objects, hence why it's false.

As an aside, calling the intern() method on any string will add it to the constant pool, so long as an equivalent string isn't there already (and return the String that it's added to the pool.) It's not necessarily a good idea to do this however unless you're sure you're dealing with strings that will definitely be used as constants, otherwise you may end up creating hard to track down memory leaks.

Solution 2 - Java

s1 and s2 are String literals. When you create a new String literal the compiler first checks whether any literal representing the same is present in the String pool or not. If there is one present, the compiler returns that literal otherwise the compiler creates a new one.

When you created String s2 the compiler returns the String s1 from the pool as it was already created before. That is the reason why s1 and s2 are same. This behaviour is called interning.

Solution 3 - Java

This phenomenon is due to String interning.

Basically, all string literals are "cached" and reused.

Solution 4 - Java

In Java, the same constant strings will be reused. So that s1 and s2 point to the same "abc" object and s1==s2. But when you use new String("abc"), another object will be created. So that s1 != str1.

Solution 5 - Java

This is due to String literals being interned. On this matter, Java documentations says:

> All literal strings and string-valued constant expressions are > interned

And this explains why s1 and s2 are the same (these two variables point to the same interned string)

Solution 6 - Java

>As string is immutable in java, all the string literals cached for the reusability.

When you create String object using new() operator, it always create a new object in heap memory. On the other hand, if you create object using String literal syntax e.g. "Java", it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.

String s1 = new String("java");
String s2 = new String("java");
String s3 = "java";
String s4 = "java";

enter image description here

Please refer this link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser84592View Question on Stackoverflow
Solution 1 - JavaMichael BerryView Answer on Stackoverflow
Solution 2 - JavaChandra SekharView Answer on Stackoverflow
Solution 3 - JavaBohemianView Answer on Stackoverflow
Solution 4 - JavazsxwingView Answer on Stackoverflow
Solution 5 - JavaGETahView Answer on Stackoverflow
Solution 6 - JavaRamesh PapagantiView Answer on Stackoverflow