Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings in general

 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
if i have a code like

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
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.
Thanks...
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sudhanshu!



Eight objects before the println statement(that's the original question in book):
1.spring (stored in s1)
2.summer(lost)
3.spring summer(stored in s2)
4.fall(lost)
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)
7.winter(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
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic