Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Location: Venezuela
Contact:

Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by javiut »

In the Page 98 they are a snippet like this.

Code: Select all

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.
Also this question is great on SO.
I think that should change the text and the # of objects created.

Best regards. :cheers:
Last edited by javiut on Sun Feb 09, 2020 10:37 am, edited 1 time in total.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by admin »

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.

javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Location: Venezuela
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by javiut »

Yes a extremely difficult topic. A errata or anything will be added? to keep some track? thanks mate. :cheers:

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by admin »

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.

javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Location: Venezuela
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by javiut »

please let me know thanks

javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Location: Venezuela
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by javiut »

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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by admin »

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.

javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Location: Venezuela
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 98

Post by javiut »

got it.

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests