File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Concatenation in System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Concatenation in System.out.println" Watch "String Concatenation in System.out.println" New topic
Author

String Concatenation in System.out.println

Naveen Haimsley
Greenhorn

Joined: Dec 16, 2012
Posts: 3
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 176
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?


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Dec 16, 2012
Posts: 3
Thanks all for the inputs.... These helped much..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Concatenation in System.out.println