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?
[HD Pg 133, Sec. 3.5.3 - garbage-collection-of-strings]
Moderator: admin
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 133, Sec. 3.5.3 - garbage-collection-of-strings]
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,
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
As per section 15.18.1,
So, in your example, the strings containing str+i will be newly created (and not cached).The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
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
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 42 guests