• 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

How many String objects will be created?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public String makinStrings() {
String s = "Fred";
s = s + "47";
s = s.substring(2, 5);
s = s.toUpperCase();
return s.toString();
}
How many String objects will be created when this method is invoked?
 
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Five is the answer.

1. "Fred";
2. "47";
3. Outcome of s + "47"
4. Outcome of s.substring(2, 5);
5. Outcome of s.toUpperCase();
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's 6.

The last return call is also creating a String object which is being returned.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three is the answer.

1. Outcome of s + "47"
2. Outcome of s.substring(2, 5);
3. Outcome of s.toUpperCase();

For an explanation, you should read the following article:

Strings, Literally
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Devashish Roy:
I think it's 6.

The last return call is also creating a String object which is being returned.



API documentation of String.toString:

This object (which is already a string!) is itself returned.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,
does that mean that "if i have two reference variable pointing to a single String object, and we have a concatenation (or any string manipulation) performed on the object using one reference, then the second reference would still point to the original unmanipulated String?"
 
Syed Salman Ahmad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred, don't you think "Fred" and "47" are not objects. I think they are!.
Because every string literal is a string object which belong to string pool.
That's why i think answer is 5 .
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi fred,


Ok in that case answer is still 5 and not 3. Technically, behind the scene, jvm also creates two String objects for "Fred" and "47".
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikash Agarwal:
does that mean that "if i have two reference variable pointing to a single String object, and we have a concatenation (or any string manipulation) performed on the object using one reference, then the second reference would still point to the original unmanipulated String?"


Yes.


The output is:
String 47
String
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Syed Salman Ahmad:
Manfred, don't you think "Fred" and "47" are not objects. I think they are!.
Because every string literal is a string object which belong to string pool.
That's why i think answer is 5 .


"Fred" and "47" are objects. But the question is:

How many String objects will be created when this method is invoked?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think Devashish Roy is right
"fred" and "47" also string objects only
as Manfred Klug said
toString() method will return same object only, no more creation of object
so total will be 5 objects

regards
Mallik Avula
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mallik,

you should really read the article I mentioned above.









There are questions you should never answer.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats correct Manfred.

Hi syed,

Though the String literals are also String Objects in Heap which points to the Literals in the Literal Pool, but being the String Literals they are picked up when the during the class-loading operation.

Thats why the answer was 3 instead of 5.


quote:How many String objects will be created when this method is invoked?



That was a good point of observation ManFred!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, toString() does NOT return a New String if this method is invoked on a String. But this does return a new string when invoked on other non-String objects.

Carey McGlone's "Strings, Literally" - article is one of the excellent articles. Guys, please go through it and i bet its worth spending your time!!
[ June 19, 2007: Message edited by: Raghavan Muthu ]
 
Syed Salman Ahmad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everybody especially Raghavan for explaining it.
 
Mahesh Kamath
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Manfred.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, ManFred is the * Man NOT to be missed here *!!

Thanks Manfred for the active replies
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic