File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Number of String objects created

Praveen john
Greenhorn

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();
7
8 return s.toString();
9
10 }
11


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:
http://www.javaranch.com/name.jsp


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.


Yours,
Bu.


all events occur in real time
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,

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

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".

Regards,
Mack.
Gitesh Ramchandani
Ranch Hand

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

regards,
gitesh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Siphiwe Madi
Ranch Hand

Joined: Aug 16, 2007
Posts: 70
Hi,

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:

http://www.javaranch.com

Thanks
Siphiwe M


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

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

Anup


I code therefore I am.
Gitesh Ramchandani
Ranch Hand

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

Check here:

http://www.coderanch.com/t/263415/java-programmer-SCJP/certification/many-String-objects-will-created

Gitesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of String objects created
 
Similar Threads
String objects created
Strings
String Test
String Objects
StringObjects