• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

help! how many String Object will be created

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reviewed the k&b,
I think the following code will be created six Strings but the answer is 3.

the code is

public String makingString(){
String s ="Fred";
s= s + "47";
s=s.subString(2,5);
s= s.toUpperCase();
return s.toString();
}

please help!
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gang,

Welcome to JavaRanch!



1- "Fred" referenced by s
2- "47" no reference
3- "Fred47" referenced by s
4- "ed4" referenced by s
5- "ED4" referenced by s

toString() does not create a new String if the the object on which it is called is String only.

I think total of 5 String created, as I know when the compiler encounters String literal, it places it to the constant pool.
 
gang zhou
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the idea , thanks , and now i am preparing scjp1.5
 
There's a city wid manhunt for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic