public class StringCreations {
public static void main(String[] args) {
String hello = "hello";/*A STRING CREATED HERE*/
for(int i=0;i<5;i++){
hello = hello + i;/*OTHER STRING CREATED IN EACH ITERATION.*/
}
System.out.println(hello);/*6 or 11 objects created at this time?? i think is 6*/
}
}
The book states that 2 objects is created in each iteration and this assertion is not correct. The book states that a String object is created at the int value but in Java 8 they would be a total of 11 objects because this would be translated into StringBuilder and later a call to String() which in each iteration would correctly created 2 objects [1 StringBuilder + 1 String] but in Java 9 and on not StringBuilder is used anymore just creating a String on each iteration for a total of 6 objects i dont think is creating a object for every int value but this is very deep on the internals of the JVM we really dont know if in fact creating and additional object is created here but seems that String is very optimized at this point and will not create a additional object here.
In resume i dont think neither the guys on SO that a additional String object is created for the int value for each iteration.
In fact i made this question on Stack and they agree with that this code in Java>=9 is creating 6 objects and in Java <9 is 11 objects you can check it out.
Yes, finding out the number of strings created is indeed a bit more complicated if you consider the implementation of the JVM. The example is just about what the code does.The book also says in this paragraph:
In this case, JLS section 15.18.1 clearly says that a compiler may avoid creating multiple strings altogether by making use of a StringBuffer. Thus, it is not possible to tell how many Strings will be created and how many will be eligible to be garbage collected.
Don’t worry, you will not get questions in the exam about garbage collection of Strings. I have talked about it here only to make you aware of the issue. Strings muddle the topic of garbage collection so much so that it is a bad idea to use them while explaining garbage collection.
Will recommend to improve this section anyway.
If you like our products and services, please help us by posting your review here.
I am not sure if it an errata because it does mention that the JVM may use StringBuilder. But an improvement is certainly required.
Either way, it has been noted and will be fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
But in the Java 11 version which is the book version is not using StringBuilder anymore you can check it out here. https://stackoverflow.com/questions/465 ... -in-java-9 in fact from Java 9 Onwards not StringBuilder is not used.
No, the book is correct about this. See the following written in Section 15.18.1 of JLS 11 (Page 588)
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
The book is also saying the same thing quoting the same section of JLS.
If you like our products and services, please help us by posting your review here.