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.
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 "
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.
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
10.The entire println you are printing to screen i.e. spring winter spring summer