Page 1 of 1

[HD Pg 133, Sec. 3.5.3 - garbage-collection-of-strings]

Posted: Mon Jan 14, 2019 2:55 pm
by OCAJO1
String str = "hello";
for(int i=0; i<5; i++){
str = str + i;
}

In the above example, does JVM cache the object str or str+i (similar to valueOf would) or actually creates 11 new string objects on the heap?

Re: [HD Pg 133, Sec. 3.5.3 - garbage-collection-of-strings]

Posted: Mon Jan 14, 2019 10:50 pm
by admin
First thing that you should remember is that all objects, whether cached or not, are always created on the heap.

As per section 15.18.1,
The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
So, in your example, the strings containing str+i will be newly created (and not cached).
This simple example proves it:
int i = 1;
String s1 = "a"+i;
String s2 = "a"+i;
System.out.println(s1 == s2); //prints false, this implies s1 and s2 point to two different objects