• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Concatenation Operator & Garbage Collection (K&B7 CH5 Q9)

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raul Saavedra wrote:As they say in German, kein Problem!


Being from Belgium, with German as one of three official languages: kein Problem (all nouns must start with a capital ) to understand "kein Problem"
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Stumbling upon the same issue as Raul, I have to make a remark. Roel repeatedly states that for the certification exams (like OCA and OCP) there is no need to know about this optimization performed by the Java compiler, nor the bytecode, and that the code can be evaluated without such knowledge.

I disagree with this statement: the answer to the question being discussed cannot be known _without_ such knowledge as above. I will argument my claim.

Roel, you correctly state the following:

Roel De Nijs wrote:
If one of them is not a compile time constant, a StringBuilder will be used to concatenate (also a compiler optimization). As shown in this snippet (followed by the decompiled code and the resulting bytecode)






Now, in the question in the book, we have the statement s=" "+i; which is looped 1000 times. Here, i is not a compile time constant, thus for each concatenation we get the following replacement code:

Meaning, for each concatenation we have one StringBuilder object and one String object created from the toString() method.
Even more: the StringBuilder(String) constructor allocates a buffer containing 16 characters. Which is a char[16] array, which is also an object.
So, per concatenation - meaning per each iteration in the loop - we have 3 NEW objects being created: a StringBuilder, the corresponding char[] array, and a String object obtained from the toString() method.

Multiplied by 1000 iterations, we get something like 3000 objects. In any case not the stated answer B (about 1000).

In the end I have to say that not only the statement in Chapter 4 is wrong in the book, but also this question from the Self Test. And if for the OCAJP exam the underlying mechanisms regarding this question are out of scope for the exam, then the question is not appropriate.

If I am missing something, then PLEASE instruct me as to how should I cast my judgement when trying to asnwer such a question. Thank you for answering me!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Radu Marius wrote:If I am missing something, then PLEASE instruct me as to how should I cast my judgement when trying to asnwer such a question. Thank you for answering me!


You should NOT take compiler optimizations into account when answering any (mock) exam questions. Just stick to the code you see and what you know about strings, objects and garbage collection!

I thought this was already mentioned very clearly in this topic, but seems it was not. Therefore I repeat it once more: You should NOT take compiler optimizations into account when answering any (mock) exam questions. Just stick to the code you see and what you know about strings, objects and garbage collection!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With regard to strings and garbage collection, this note is quite an important one (from K&B7, page 201)

K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.

 
Radu Marius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Roel, thank you for your advice. I will follow it and I hope it holds true during the exam, if such a question should pop up!...

Anyways, I have not seen the issue from Chapter 4 and the proposed correction (the one that leads to this question's difficulties) being mentioned on the errata. Shouldn't it be added there?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Radu Marius wrote:Anyways, I have not seen the issue from Chapter 4 and the proposed correction (the one that leads to this question's difficulties) being mentioned on the errata. Shouldn't it be added there?


And what about this errata item?
 
Radu Marius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I see, indeed it is posted on the errata. Your are correct, thank you for your promptness!
reply
    Bookmark Topic Watch Topic
  • New Topic