Hi, I recently posted in this thread, explaining from my understanding what's happening in the code. However, I've been wondering what is actually true about this. In the K&B book, pg. 419, there is this code sample:
Now they say that there are EIGHT objects created altogether, but I am counting NINE, based on my understanding. I will list them: STRING LITERALS 1. "spring " // line 1 2. "summer " // line 2 3. "fall " // line 3 4. "winter " // line 5 5. " " // line 6 (this is the one I'm wondering about) RUNTIME STRING OBJECTS 6. "spring summer " // created in line 2 7. "spring fall " // created in line 3 8. "spring summer spring " // created in line 4 9. "spring winter " // created in line 5
Now, is the " " counted as a compile-time constant and added to the String pool? Why or why not? I am assuming that it is, but if not, then I would guess that's because it's not declared in an assignment statement. Can anyone help clear this up please? Thanks!
hi, there are eight objects are created. s1 ,s2 are reference variables. s1 - 1 s2 - 2 object but s2 = s1+summer - 3 s1.concat("fall") - 4(generates spring fall but it lost did not assign to any variable-5) s2.concat(s1) -6 the generated string for above method-7(will lost) last one spring winter -8 total 8 objects are created.but 2 are lost
I would guess there are 11 String objects created:
Compile time �spring � // line 1 �summer � // line 2 �fall � // line 3 �winter � // line 5 � � // line 6
Runtime �spring summer� // line 2 �spring fall � // line 3 �spring summer spring � // line 4 �spring winter � // line 5 �spring winter � // line 6 �spring winter spring summer� // line 6
From those 11 objects, only 3 are ever referenced by variables (�spring �, �spring summer� and �spring winter �). And when this code finishes, only 2 are still referenced by variables (�spring summer� and �spring winter �).
Say the compiler optimizes the String concatenations. �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.� This might result in something like this:
Then the following String objects would be created: Compile time �spring � // line 1 �summer � // line 2 �fall � // line 3 �winter � // line 5 � � // line 6
Runtime �spring summer� // line 2 �spring fall � // line 3 �spring summer spring � // line 4 �spring winter � // line 5 �spring winter spring summer� // line 6
This still results in 10 String objects. It doesn�t matter if they are discarded right away since the question was how many String objects are created.
Now, concerning the K&B book, the question was: how many String objects and how many reference variables are created prior to the println statement. This is then: 2 reference variables (s1 & s2) and 8 objects (�spring �,�summer �,�fall �,�winter �,�spring summer�, �spring fall �,�spring summer spring � and �spring winter �).
Eric, " " if placed in an assignment statement, it is counted as a String instance. i.e String s=" "
However even if a integer no is placed in System.out.println statement its first converted to String using toString() method. So i guess your point stands valid for " " to be considered as a String constant.
But since " " is a part of String passed to the PrintStream, it will be flushed out after displaying the String. Since programs have many print statements it would not be of no use to store Strings in the print statements.
9 String objects are created. 2 String objects are lost.
A String object with content "spring winter " and another with content "spring winter spring summer " (mind the spaces) are created. They do not count for the number of String objects created. The latter is printed.
I hope this clarifies. [ September 10, 2008: Message edited by: Ronald Schild ]
Joined: Sep 09, 2008
Hey Ronald, You are correct but since the code was printed without the class, I assume they didn't count the " ", which is loaded when the class is loaded. They are basically just asking how much objects are created in:
Anyway, I think this is pretty clear now...
Joined: Jun 09, 2008
Originally posted by Stijn Janssens: Hey Ronald, You are correct but since the code was printed without the class, I assume they didn't count the " ", which is loaded when the class is loaded. They are basically just asking how much objects are created in:
Anyway, I think this is pretty clear now...
It's logical to assume they did. Though with the presence of the last line, the first line could not have been executed without " " existing.
And it's fun to nitpick and try to cause some more gray hairs for the authors, I think we can all agree there .
Joined: Jul 11, 2006
Ah, it's much more clear to me now. Thanks especially to you, Stijn Janssens, for pointing out to me that they had asked about the number of objects created prior to the println statement. Now I can (and will) continue on with my life without worrying about how the compiler handles String creation and concatenation in places such as a println statement. Thank you guys all for your help in exploring this problem and all your ideas of what is really happening.