3

Strings with no corresponding objects in string constant pool while using intern method is returning the reference of the same object present in the heap. Isn't supposed to return the reference of an entirely different object which has been newly created by the intern method inside the String constant pool? Consider the given piece of code.

class  Test{
public static void main(String[] args) {
    String s1 = new String("durga");
    String s2 = s1.concat("software");
    String s3 = s2.intern();
    System.out.println(s2 == s3);
    String s4 = "durgasoftware";
    System.out.println(s3 == s4);
 }
}

input: deep (master *) LanguagePackageInJava $ javac Lecture14.java
deep (master *) LanguagePackageInJava $ java Test
output: true
true

How is the output of the first print statement true? According to me s2 is the object of the heap area while s3 should be the object of the string constant pool area. How can both of them point to the same object?

jason0x43
  • 3,363
  • 1
  • 16
  • 15
  • 1
    Another fact: Move the statement `String s4 = "durgasoftware"` to the beginning of the method. Now it prints `false`, then `true`! I am interested in the explanation, too. – Seelenvirtuose Sep 05 '18 at 07:36
  • “s2 is the object of the heap area” not necessarily, the javadoc doesn’t state anywhere that concat has to create a new object or that can’t be put in the intern pool. You should just never compare strings with ==, then you’ll be fine. – Erwin Bolwidt Sep 05 '18 at 07:41
  • Might be helpful [Difference between java 6 and 7](https://javarevisited.blogspot.com/2016/07/difference-in-string-pool-between-java6-java7.html) – ArunKumar M N Sep 05 '18 at 07:45
  • @ErwinBolwidt at runtime any object which has been created will be at the heap...isn't? – Grounded Snippets Sep 06 '18 at 17:31

2 Answers2

1

As you can see in the java doc:

Returns a canonical representation for the string object. [...] When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

So in fact it first interns the string referenced by s2 and then returns that reference (storing it in s3). This results in s2 and s3 pointing to the same object.

If the string pool already contains the string (try it out by moving the statement String s4 = "durgasoftware" to the top of the method), this is returned instead and the string referenced by s2 is not interned. That yields another result.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
Kroxitrock
  • 35
  • 11
0

The reason is that the value of s2 is used as "intern". The object simply is shoved into the pool for reuse. Below I expect different outcomes, by first interning the same string of s0.

String s0 = "durga".concat("software").intern();
String s1 = "durga";
String s2 = s1.concat("software");
String s3 = s2.intern();
System.out.println(s2 == s3); // false
System.out.println(s3 == s0); // true

Through history the implementation of intern changed. At the beginning the string pool was in the "permanent memory" possibly cluttering that memory. XML tags that were interned by XML parsers consequently used their own mapping instead.

I hope the code indeed yields false+true.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138