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 Number of String objects created 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 "Number of String objects created" Watch "Number of String objects created" New topic

Number of String objects created

Praveen john

Joined: Sep 02, 2007
Posts: 9
1 public String malkestring()
2 {
3 String s = "fred";
4 s = s + "47";
5 s = s.substring(2,5);
6 s= s.toUpperCase();
8 return s.toString();
10 }

Q. How many String objects are created?
A 1
B 2
C 3
D 4
E 5
F 6

The answer is given as c ie 3.

How can it be 3. According to the String class its objects are immutable.
So accordingly

Line 3: "fred" is one object
Line 4: "47" is one object
Line 4: "fred47" is one object
Line 5: "ed4" is one object
Line 6: "ED4" is one object

So Adding all together a total of 5 objects are created.

Please help me with this problem as to which is the right answer.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy "coroner.john kumar" !

Thanks for your first contribution to this forum and...

Welcome to the Ranch!

Hope you'll enjoy.

Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:

So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.

I'm posting this because I am one of the moderators of this forum.


all events occur in real time
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

I too see 5 objects being created here. Is there anything we are missing ?
Mack Stevens

Joined: Aug 31, 2007
Posts: 17
Sorry to say that I dont know the exact answer, but i can clarify it to some extent.You are forgetting the concepts of Garbage Collection and also you are not noticing that return statement in your code is also returning a String object(i.e toString() method).so i think the answer is "2".

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
i'm also confused with this question, please help.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15084

Praveen, please check your private messages. You can see them by clicking My Profile.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Siphiwe Madi
Ranch Hand

Joined: Aug 16, 2007
Posts: 70

There was a post regarding the number of String objects created, i think it will help to answer your question.

Please refer to the link below to access it:

Siphiwe M

Regards, Siphiwe Madi
[SCJP, SCWCD, __] Next ... scjwsd
Anup Jadhav

Joined: Jun 09, 2007
Posts: 9
The total number of objects created are "6".


I code therefore I am.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Total 3 objects are created,

Check here:

I agree. Here's the link:
subject: Number of String objects created
It's not a secret anymore!