aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String objects are REALLY created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many String objects are REALLY created?" Watch "How many String objects are REALLY created?" New topic
Author

How many String objects are REALLY created?

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
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!


Studying for SCJP 6
SaiRishi Anusuri
Greenhorn

Joined: Mar 18, 2008
Posts: 14
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
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Stijn Janssens
Greenhorn

Joined: Sep 09, 2008
Posts: 12
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 �).


<a href="http://zerotreize.com/#/blog/java-certification-matters" target="_blank" rel="nofollow">Java Certification matters?</a><br /><a href="http://www.zerotreize.com" target="_blank" rel="nofollow">Zerotreize.com</a>
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
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.


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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 ]

Java hobbyist.
Stijn Janssens
Greenhorn

Joined: Sep 09, 2008
Posts: 12
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

Joined: Jun 09, 2008
Posts: 117
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

Joined: Jul 11, 2006
Posts: 143
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many String objects are REALLY created?
 
Similar Threads
how many String objects are created
How many objects are created ?
How many String Object are created in the following code ??
How many String objects are created.
Number of String objects created