• 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

How many String objects are created.

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I thought i understood how string objects are created but now i have some doubts.

Consider the following exmamples

Example 1


Example 2


Example 3


Example 4 (From K&S)


I am interested to know how many String objects are created in each of the above examples. I am confused as to whether to list the number of objects created in the constant pool or the total number of objects created (including temporary discarded ones)

For example 1, i initially thought 5 objects, i.e.



But then thinking about it i am not really sure because for example will the compiler concatenate `"A" + "B"` as one object? i.e creating 7 objects?


* Are the number of objects created in example 2 exactly the same as Example 1 but with an additional string object?
* I think 3 objects are created in example 3. Is this correct? Also, do s1 and s2 both refer to the same string 'A' in the constant pool even though they are different objects?

I thought i understood the answer for example 4 as it was explained in the book. I posted a question on another site and the responses there seem to suggest that the compiler doesnt create the temporary objects as discussed in the book - See this discussion http://stackoverflow.com/questions/8316687/literal-string-creation-vs-string-object-creation Please can someone clarify this.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
Consider the following exmamples

Example 1



For example 1, i initially thought 5 objects, i.e.



But then thinking about it i am not really sure because for example will the compiler concatenate `"A" + "B"` as one object? i.e creating 7 objects?




Interestingly, it is neither option.... String literals are compile time constants. Concatenation of compile time constants can be done at compile time. So, the compiler will generate code that will instantiate one string object -- "ABCD".

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
Consider the following examples

Example 1


Example 2


Example 3


* Are the number of objects created in example 2 exactly the same as Example 1 but with an additional string object?



Yes. Example two creates an additional string object.

O. Ziggy wrote:
* I think 3 objects are created in example 3. Is this correct? Also, do s1 and s2 both refer to the same string 'A' in the constant pool even though they are different objects?



Two objects. The s1 reference refers to the same object that was used to create the s2 reference. So, two objects, one referenced by s1, and the other referenced by s2.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
I thought i understood the answer for example 4 as it was explained in the book. I posted a question on another site and the responses there seem to suggest that the compiler doesnt create the temporary objects as discussed in the book - See this discussion http://stackoverflow.com/questions/8316687/literal-string-creation-vs-string-object-creation Please can someone clarify this.




Having just read it, that link seems to explain exactly what I just explained -- wish I read it first...

What do you need clarified?

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things i would like clarified please.

I need to know how to know how many objects are created. I did not know that there is a difference in the way strings are constructed if the string is a literal. For example, in your first reply you say that String s1 = "A" + "B" + "C" + "D"; will result in one String object being created. I thought 4 would be created but one will end up in the constant pool.

In the following example,



In the book it is mentioned that a total of eight objects will be created in the above code (excluding those in the System.out.println). What is the difference in the above compared to s1 = "A" + "B" + "C" + "D"; Shouldnt the following initialisations have also been treated as string literal constants?



Thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:



In the book it is mentioned that a total of eight objects will be created in the above code



Ok, so this part makes sense -

1) spring
2) summer
3) fall
4) winter
5) s1
6) s2
7) object created by - s1.concat("fall ");
8) object created by - s2.concat(s1);

Regards,
Dan


 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote: What is the difference in the above compared to s1 = "A" + "B" + "C" + "D";



Henry explained -

Concatenation of compile time constants can be done at compile time. So, the compiler will generate code that will instantiate one string object -- "ABCD".



Regards,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day,

Supporting Henry’s statement my beloved outstanding book - Java 2 by Bill Brogden says on page #39 -

When the complier finds an expression in which a string appears in association with a + operator, it turns all items in the expression into strings, concatenates the strings, and creates a new String Object.



Regards,
Dan
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan Drillich wrote:

O. Ziggy wrote:



In the book it is mentioned that a total of eight objects will be created in the above code



Ok, so this part makes sense -

1) spring
2) summer
3) fall
4) winter
5) s1
6) s2
7) object created by - s1.concat("fall ");
8) object created by - s2.concat(s1);



Weird. I count ten string objects.

1. "spring "
2. "summer "
3. "fall "
4. "winter"
5. " "
6. concated result from line 2
7. returned from concat() method from line 3
8. returned from concat() method from line 4
9. concated result from line 5
10. concated result from line 6 to be sent to println()

Now, of course, if you don't count the println, then you lose two -- #5 and #10.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:I need to know how to know how many objects are created. I did not know that there is a difference in the way strings are constructed if the string is a literal. For example, in your first reply you say that String s1 = "A" + "B" + "C" + "D"; will result in one String object being created. I thought 4 would be created but one will end up in the constant pool.



In example #1, the compiler will concat it at compile time, since literals are compile time constants ... basically, it is like you did this...

String s1 = "ABCD";

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:Shouldnt the following initialisations have also been treated as string literal constants?




You need to understand the difference between an object and a reference. And for that matter the difference between a literal and a compile time constants.

Basically, in line 1, "spring " is a literal, and hence, is a compile time constant.

However, s1 is *not* a compile time constant, even though it was assigned an object that was a compile time constant. So, in line two, since s1 is not a compile time constant, the expression is not a constant expression, and s2 must be calculated at runtime. Same response applies for line three.



Now, to confuse matters more; if you had done this...



then interestingly, s1 is a compile time constant (but not a literal), and the compiler can do the concat (in line 2) at compile time. Weird huh?

And of course, if s1 is final, line 3 won't compile at all.


Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok one final question. I had always thought that compile time constants are those that are declared public final. Is this not correct?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Variables declared with the modifiers public final static forms a constant.
Local variables declared final within the methods are constants within the method scope.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:Ok one final question. I had always thought that compile time constants are those that are declared public final. Is this not correct?



No. Please don't confuse application constants and compile time constants. An application constant may be a compile time constant, and vica versa, but they are not the same thing.

Anyway, I did a quick write up of what is a compile time constants a while back. Hope this helps...

https://coderanch.com/t/454384/java/java/compile-time-constant

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic