File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Test" Watch "String Test" New topic

String Test

Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52

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

Thanks in advance

J 4 Java
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
I'm having trouble with String creations also but I think the above code will create 5 String objects.

They are:


Please correct me if I am wrong. What do you think the answer is?
[ May 30, 2008: Message edited by: Mustafa Musaji ]

SCJP 5.0 - Passed
niveditha ramana

Joined: May 31, 2008
Posts: 6
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"

with regards<br />niveditha<br /> <br />"The pessimist borrows trouble; the optimist lends encouragement"<br />- William Arthur Ward
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46429
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.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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..

Thanks<br />Dinesh
Yaroslav Ashurin

Joined: Apr 25, 2008
Posts: 22

Hey, Sarath, what's up.

Please check the thread:

SCJP 1.5
niveditha ramana

Joined: May 31, 2008
Posts: 6
thank you Yaroslav Ashurin
i got it clear.

thanks lot for Campbell Ritchie
I agree. Here's the link:
subject: String Test
It's not a secret anymore!