Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Radu Marius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be an error on page 305, at the Self Test for Chapter 5. Question number 9. It is indicated in the book that the correct answer is B, meaning About 1000 objects.
However, the correct answer seems to be C, About 2000 objects.

Two ways of reasoning for the C answer:

1. From what I have read, what really goes on under the hood while concatenating Strings using the "+" operator is that the operation is actually performed using an underlying StringBuilder object, through the append() method. So for this line: s=" "+i;, we would get a new StringBuilder object created with the " " being passed into the constructor, and then invoking the append() method to concatenate the int i. Finally, a second String object is created by calling the toString() method on the StringBuilder object and assigning it to the s variable.
So this implies 2 objects per concatenation operation. Multiplied by 1000, it gives us about 2000 objects.

2. The second way of reasoning would be: if the underlying implementation of the concatenation does not use an underlying StringBuilder object, then this line: s=" "+i; would imply the following objects:
- the " " String object - reused in every iteration;
- the String object derived from transforming the int i to a String;
- the concatenated String.
And again this would imply 2 objects multiplied by 1000, which is about 2000 objects.

Please help me understand this issue if I am getting it wrong.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Radu Marius,

First of all, a warm welcome to CodeRanch!

Radu Marius wrote:Please help me understand this issue if I am getting it wrong.

Have a look at this topic! It has a very detailed discussion about this question and I'm pretty sure it will clear your doubts. Warning: it's quite a long thread and the thread includes wrong/misleading code in some parts because of confusion through the evolution of the clarifications, so make sure you go all the way to the end where the actual issue is finally truly clarified.

If you still have doubts after reading this thread, don't hesitate to let us know by simply replying to this thread.

Hope it helps!
Kind regards,
Roel
 
Radu Marius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel for pointing out the issue.

I have read through the thread and from what I understand what is in fact wrong is not the question but the information presented in Chapter 4, at page 237 of the electronic edition, precisely this bit of text:
" Add the values of b and c together, and then take the sum and convert it to a String and concatenate it with the String from variable a."
which should be changed to something like:
"Add the values of b and c together, and then take the sum and concatenate it with the String from variable a."

Am I correct so far?

There is just a little misunderstanding remaining in my mind, which _seems_ to be somewhat of a contradiction between your understanding of the matter and Raul's understanding of the matter. In your reply at 12/30/2014 3:39:49 PM, it is shown that Raul understands that the avoindance of using a StringBuilder is caused by the String literal " " which comes to the left of the first plus. And that if it were a String variable, then that would have changed matters and a StringBuilder would have been used, leading again to the 2000 objects answer.

But in your reply at 12/30/2014 5:41:11 AM, you are saying "If one of them is not a compile time constant, a StringBuilder will be used to concatenate ". ---> You are saying "one of them", and not "the one to the left of the first plus".

So, my question is, what exactly triggers the use of a StringBuilder? Any of the operands not being a compile time constant?, or only the first operand not being a compile time constant?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radu Marius wrote:Am I correct so far?

Yes, you are!

Radu Marius wrote:So, my question is, what exactly triggers the use of a StringBuilder? Any of the operands not being a compile time constant?, or only the first operand not being a compile time constant?

That's completely irrelevant for the OCA (and OCP) exams. You should not know about which optimizations are performed by the compiler nor about which code triggers a certain compiler optimization.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radu Marius wrote:So, my question is, what exactly triggers the use of a StringBuilder? Any of the operands not being a compile time constant?, or only the first operand not being a compile time constant?

I can't emphasize this enough: for the certification exams (like OCA and OCP) you do not need to know about this optimization performed by the Java compiler (nor the bytecode)! You just have to evaluate the (source) code you see.
 
Radu Marius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your feedback! I have posted my reply on the matter at this topic that you pointed me to.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Similar Threads