• 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

String Test

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

How many String objects will be created when this method is invoked?and how ?

Thanks in advance
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble with String creations also but I think the above code will create 5 String objects.

They are:

Fred
47
Fred47
ed4
ED4

Please correct me if I am wrong. What do you think the answer is?
[ May 30, 2008: Message edited by: Mustafa Musaji ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = "Fred";
s = s + "47";
s = s.substring(2, 5);
s = s.toUpperCase();

i think only four objects were created

1. "Fred"
2. s now refers the object s+"47" ->"Fred47"
3. "ed4"
4. lastly object "ED4"
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this same question has come up before; please quote where you got the question from. And remembering what was discussed when it came up before, the answer is simple: you have both got it wrong. Read the question carefully and give the correct answer.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from Kathy and sierra book

i think answer is 2 string objects created

1. String s ="fred"
2. s=s+"47"

please correct me if i am wrong..
 
Greenhorn
Posts: 24
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Sarath, what's up.

Please check the thread: https://coderanch.com/t/410223/java/java/String-objects
 
niveditha ramana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Yaroslav Ashurin
i got it clear.

thanks lot for Campbell Ritchie
 
reply
    Bookmark Topic Watch Topic
  • New Topic