Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Objects created in memory

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator

See: The SCJP Tip Line Strings, Literally
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • 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.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic