• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Number of String objects created

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

regards,
gitesh
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Praveen, please check your private messages. You can see them by clicking My Profile.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Total 3 objects are created,

Check here:

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

Gitesh
 
It was the best of times. It was the worst of times. It was a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic