Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Concatenation in System.out.println

 
Naveen Haimsley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does System.out.println("Something goes here"); create a new String object??

If not, then will System.out.println(object1+object2); create a new String object? Here we are using '+' to concatenate which normally would create a new object.

Please let me know if i have some misconception.....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naveen Haimsley wrote:Does System.out.println("Something goes here"); create a new String object??

It depends...

If the literal "Something goes here" is used elsewhere in your code prior to this point, then no new object needs to be created. String literals are slightly different beasts that most other objects.

Naveen Haimsley wrote:If not, then will System.out.println(object1+object2); create a new String object? Here we are using '+' to concatenate which normally would create a new object.

I would say it potentially creates THREE new objects...

object1's "toString()" method gets called, creating one String object. object2's "toString()" method gets called, creating another String object.

Then those two strings are concatenated, creating a third String Object.

I may be wrong, but that's how I see it.
 
Kevin Florish
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:object1's "toString()" method gets called, creating one String object. object2's "toString()" method gets called, creating another String object.
Then those two strings are concatenated, creating a third String Object.
I may be wrong, but that's how I see it.


Are object1 and object2 not just passed as byte arrays to PrintStream for output?
 
Henry Wong
author
Marshal
Pie
Posts: 21507
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Naveen Haimsley wrote:If not, then will System.out.println(object1+object2); create a new String object? Here we are using '+' to concatenate which normally would create a new object.

I would say it potentially creates THREE new objects...

object1's "toString()" method gets called, creating one String object. object2's "toString()" method gets called, creating another String object.

Then those two strings are concatenated, creating a third String Object.

I may be wrong, but that's how I see it.


A couple more points... If object one or object two are already string objects, then no new string objects are created -- even if the toString() method are called. Also, if the two objects are compile time constant variables, then the concat is done at compile time too -- and the result is just another string that may be already loaded in the string pool.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21507
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Florish wrote:
fred rosenberger wrote:object1's "toString()" method gets called, creating one String object. object2's "toString()" method gets called, creating another String object.
Then those two strings are concatenated, creating a third String Object.
I may be wrong, but that's how I see it.


Are object1 and object2 not just passed as byte arrays to PrintStream for output?


No. If there is only one object, then the compiler will send it to either the prinln() method that takes a string or an object (ie. generate code for), depending on whether the compiler knows if it is a string object or not. If it goes to the println() method that takes an object, all that method does is call the toString() method on that object, and route it to the other method.

In this case (assuming not compile time constant), it isn't a single object. The string concat is actually syntactic sugar of a group of operations that include creating a string builder, appending the string operands, and generating a string for the println() method.

Henry
 
Naveen Haimsley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the inputs.... These helped much..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic