• 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

number of strings created in concatenation operation.

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i have a doubt in string concatenation.

1) how many strings will be created when below line is executed?


will the system create a string literal "abc" and then a string object on literal "abc"? so will system create 2 strings?

2)how many Strings will be created when below line is executed?



it will be good if anyone can give little explanation on how the strings will be created.
thanks.

regards,
Pratz
 
Saloon Keeper
Posts: 15630
366
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals aren't created when the line that references them is being executed. String literals are created when a class is loaded.

Your first example will just create one new String. Your second example will create a new String for s2, and a new String for each concatenation operation, so that's 3 new objects in total. Note that one of those (the String created by s1 + s2) will become eligible for garbage collection after the expression is finished, because there are no references to it anymore.
 
prateek sharmaa
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Stephan, that was very much explanatory.

i have one more question that you said String literals are created at the time of class loading so shouldn't we count it as an object?

thanks,
Pratz
 
Marshal
Posts: 79471
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the String literal "abc" is an object in its own right. But you said

when below line is executed

The String literal is loaded as an object into the heap/String literal pool before that line is executed.
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prateek sharmaa wrote: . . .

2)how many Strings will be created when below line is executed?

. . .

Which line? Do you mean all four lines together? I shall let you work it out. I shall also refer you to this very useful JavaRanch Journal article.
 
prateek sharmaa
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, my mistake
so now can i say that there will be 2 objects in memory after execution of below line?



one is String literal and other is new String object. right?
 
Stephan van Hulst
Saloon Keeper
Posts: 15630
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, at least those two objects will be in memory, of which the object created from the String literal will be stored in the String pool on the heap.
 
prateek sharmaa
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys. now i am clear about how string objects are created

thanks,
Pratz
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:... and a new String for each concatenation operation, so that's 3 new objects in total. Note that one of those (the String created by s1 + s2) will become eligible for garbage collection after the expression is finished, because there are no references to it anymore.


You're right that the concatenation will create two objects but they won't both be Strings. One will be a StringBuilder. The compiler is clever enough to optimise that bit of code into

In fact the compiler has the option to optimise it in any way it likes, so future versions of Java may optimise it differently. That's why it's recommended to always write

rather than

If a later Java version comes up with a better optimisation, the first bit of code will use that optimisation, but if you're explicitly using a StringBuilder then it won't.
 
Stephan van Hulst
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh cool. I think I've heard this before, but I totally forgot. Thanks Joanne!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:In fact the compiler has the option to optimise it in any way it likes, so future versions of Java may optimise it differently. That's why it's recommended to always write


Hmmm, "overloaded operator as a layer of indirection"... When will it end?

Winston
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to know about that optimisation.
 
Your mother was a hamster and your father was a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic