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 Strings in general Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings in general" Watch "Strings in general" New topic

Strings in general

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 235

Hi all,
if i have a code like

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

and,I am asked how many objects are created,what should i answer.Actually this is an example from K&B book,and there the answer is eight,but what about the string
in the println method?even there we get a string.
Please correct me if i am wrong.I just want the answer i am expected to give in a SCJP 6.0 exam for this kind of question.
Please guide me through.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

I disagree with eight. I see ten:
1) "spring "
2) "summer "
3) "spring summer "
4) "fall "
5) "spring fall " - ignored but still created
6) "spring summer spring" - ignored but still created
7) "winter "
8) "spring winter "
9) " "
10) "spring spring summer "

The last two are probably missed in the count.

How To Ask Questions How To Answer Questions
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 235

Thanks Rob,
But i am just concerned about my SCJP exam.If such a question is asked,what should i do.Should i skip the println statement and count just the objects in the code above println()?
Any suggestions would be helpful and much appreciated.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

I'll move this thread to our SCJP forum, where Bert Bates himself is hanging around. I'm sure he (or someone else) can give you a much better answer than I can.
Sam Hazim

Joined: Jul 19, 2011
Posts: 26
I covered this chapter some weeks back, but if I recall correctly doesn't the question say something like 'there are eight objects created BEFORE the final System.out.println?'

There are 8 Objects at that point, and 10 Objects once the entire section runs.
Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40

Hi Sudhanshu!

Eight objects before the println statement(that's the original question in book):
1.spring (stored in s1)
3.spring summer(stored in s2)
5.spring fall(due to result of s1.concat("fall") but not stored anywhere, so lost)
6.spring summer spring(due to result of s2.concat(s1) but not stored anywhere, so lost)
8.spring winter(stored in s1. at this point, "spring" attached to s1 is lost)

so finally, only two are stored, all others are lost. Including println statement, there are 10 String objects
9." "
10.The entire println you are printing to screen i.e. spring winter spring summer
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 235

Thanks a lot to everyone.
I agree. Here's the link:
subject: Strings in general
It's not a secret anymore!