This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Objects count Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects count" Watch "String Objects count" New topic

String Objects count

Nimit Shah
Ranch Hand

Joined: Jul 02, 2007
Posts: 55
See the following code.

11. public String makinStrings() {
12. String s = "Fred";
13. s = s + "47";
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
Answer: C

I thought that the answer would be E i.e. 5 String objects created.

Can anybody please explain me how comw the answer is C?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi Nimit,

hava a look here.
[ July 09, 2007: Message edited by: Manfred Klug ]
I agree. Here's the link:
subject: String Objects count
It's not a secret anymore!