• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How many String objects are REALLY created?

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

hope this help
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding ,

Compile time String constant : 5 - "spring", "summer", "fall", "winter" & " " .

Run time total 5 String object created but , 2 object are lost, so finally 3 String objects remained - s1, s2 and s1 + s2 .

Total = 8
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 �).
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe 9 String objects are created.

" " is a String constant that is created when the class is loaded, the String object is referenced from the String constant pool.

I've stated this earlier as well in this thread.

----------------------------------- edit

Step by step, I believe the following occurs:

* The class that contains the code is loaded.
The String constants
1. "spring "
2. "summer "
3. "fall "
4. "winter "
5. " "

Are created and referenced by the String constant pool.

The code runs:



A String reference variable s1 is declared and initialized to refer to an existing String object. 5 String objects are created so far.

s1 ----> "spring "



A String reference variable s2 is declared and initalized to refer to a String object that is created and that contains the content "spring summer ". 6 String objects are created so far.

s1 ----> "spring "
s2 ----> "spring summer "



A String object is created with the content "spring fall " and is lost, because it is not referred.

s1 ----> "spring "
s2 ----> "spring summer "

7 String objects are created.
1 String object is lost.



A String object is created with the content "spring summer spring " and is lost, because it is not referred to.

s1 ----> "spring "
s2 ----> "spring summer "

8 String objects are created.
2 String objects are lost.



A String object with content "spring winter " is created and s1 is set to refer to it. The original string s1 referred to is not lost, because it is referred to by the String constant pool.

s1 ----> "spring winter "
s2 ----> "spring summer "

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 ]
 
Stijn Janssens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic