This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are String objects created in System.out.println Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are String objects created in System.out.println Statement" Watch "Are String objects created in System.out.println Statement" New topic
Author

Are String objects created in System.out.println Statement

Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
I want to know whether String objects created in System.out.println Statement or not.

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println("Ans="+s1 + " " + s2);

Uptil now whatever discussions we had on this topic were prior to the System.out.println Statement.


SCJP 1.5
www.licexpadvice.com
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
Thumb rule - Whenever concantination of strings are done , a new object is created.
So answer for your questions is Yes


My Techie Blog - Java Questions
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
System.out.println("Ans="+s1 + " " + s2);

But in the above Statement how many Objects will be created.
According to me 3->
1. Ans=
2. " "
3. Entire Statement inside System.out.println.

I am not sure whether this is right or wrong..just want to confirm.
AKINLEYE ADEDAMOLA
Greenhorn

Joined: Sep 08, 2008
Posts: 10
if a new object are created at every System.out.println() method does it mean that it does not check the string pool to see if the literal already exist
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Hrishikesh Maluskar:
I want to know whether String objects created in System.out.println Statement or not.



I think , Yes, Strings are created inside The System.out.print() statements ! You may have confusion , because , people thinks , whatever we write in side System.out.println(), flushes out !, It may flush out , but not Garbage Collected !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Originally posted by Hrishikesh Maluskar:
System.out.println("Ans="+s1 + " " + s2);

But in the above Statement how many Objects will be created.
According to me 3->
1. Ans=
2. " "
3. Entire Statement inside System.out.println.

I am not sure whether this is right or wrong..just want to confirm.


2 String objects will be created when the class is loaded that contains this line, with content "Ans=" and " ".

3 String objects will be created in the process to build the printed String.
str1 > "Ans=" + s1
str2 > str1 + " "
str3 > str2 + s2


Java hobbyist.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are String objects created in System.out.println Statement