• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects created in memory

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is related 2 scjp. please someone help me.......

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

Please explain me how many objects are created in the memory and what are thay.
Please consider the bold line too
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is asked more than ten times in this forum....
do a search in this forum......
one of them is here
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nisal Panagoda wrote:This is related 2 scjp. please someone help me.......

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

Please explain me how many objects are created in the memory and what are thay.
Please consider the bold line too


String s2=s1+"summer";
will be converted to
String s2 = (new StringBuilder(String.valueOf(s1))).append("summer").toString();

System.out.println(s1+" "+s2);
will be converted to
System.out.println((new StringBuilder(String.valueOf(s1))).append(" ").append(s2).toString());
And all other explanations are given in other posts and K&B book.
And it depends on you, whether you want to count only String objects or StringBuilder objects also.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I guess this conversion will not happen here as such conversion only works for constants strings , not for variables

"
String s2=s1+"summer";
will be converted to
String s2 = (new StringBuilder(String.valueOf(s1))).append("summer").toString(); "

Thanks


 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

See: The SCJP Tip Line Strings, Literally
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhay Agarwal wrote:Hi

I guess this conversion will not happen here as such conversion only works for constants strings , not for variables

"
String s2=s1+"summer";
will be converted to
String s2 = (new StringBuilder(String.valueOf(s1))).append("summer").toString(); "

Thanks




It happens when you use + to concatenate two String objects. Just write this code and compile and see the .class file in a decompiler.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic