• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Are String objects created in System.out.println Statement

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thumb rule - Whenever concantination of strings are done , a new object is created.
So answer for your questions is Yes
 
Hrishikesh Maluskar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic