| Author |
Are String objects created in System.out.println Statement
|
Hrishikesh Maluskar
Ranch Hand
Joined: Jun 19, 2008
Posts: 114
|
|
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
|
<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
|
 |
Hrishikesh Maluskar
Ranch Hand
Joined: Jun 19, 2008
Posts: 114
|
|
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: 2896
|
|
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.
|
 |
 |
|
|
subject: Are String objects created in System.out.println Statement
|
|
|