• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number of strings created in concatenation operation.

 
prateek sharmaa
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49466
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Pie
Posts: 5912
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks a lot guys. now i am clear about how string objects are created

thanks,
Pratz
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh cool. I think I've heard this before, but I totally forgot. Thanks Joanne!
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to know about that optimisation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic