File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes number of strings created in concatenation operation. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "number of strings created in concatenation operation." Watch "number of strings created in concatenation operation." New topic
Author

number of strings created in concatenation operation.

prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
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

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: May 15, 2010
Posts: 38
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: May 15, 2010
Posts: 38
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

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: May 15, 2010
Posts: 38
thanks a lot guys. now i am clear about how string objects are created

thanks,
Pratz
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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.


Joanne
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Huh cool. I think I've heard this before, but I totally forgot. Thanks Joanne!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You need to know about that optimisation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: number of strings created in concatenation operation.