Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of String objects created

 
Praveen john
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I too see 5 objects being created here. Is there anything we are missing ?
 
Mack Stevens
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm also confused with this question, please help.

regards,
gitesh
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen, please check your private messages. You can see them by clicking My Profile.
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anup Jadhav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The total number of objects created are "6".

Anup
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Total 3 objects are created,

Check here:

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

Gitesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic