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?
Mahesh Kamath
Greenhorn
Joined: Mar 02, 2006
Posts: 3
posted
0
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();
Amrit Kashyap
Ranch Hand
Joined: Apr 23, 2006
Posts: 44
posted
0
I think it's 6.
The last return call is also creating a String object which is being returned.
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
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:
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.
Vikash Agarwal
Greenhorn
Joined: Jun 05, 2007
Posts: 12
posted
0
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
Joined: Apr 03, 2007
Posts: 14
posted
0
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
Joined: Apr 23, 2006
Posts: 44
posted
0
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
Joined: Jun 04, 2007
Posts: 377
posted
0
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
Joined: Jun 04, 2007
Posts: 377
posted
0
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?
Mallik Avula
Ranch Hand
Joined: Nov 30, 2006
Posts: 86
posted
0
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
Thanks & Regards<br />Mallik Avula<br />SCJP1.4
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi Mallik,
you should really read the article I mentioned above.
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?
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
Joined: Apr 03, 2007
Posts: 14
posted
0
Thanks Everybody especially Raghavan for explaining it.