aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String objects will be created? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "How many String objects will be created?" Watch "How many String objects will be created?" New topic
Author

How many String objects will be created?

Syed Salman Ahmad
Greenhorn

Joined: Apr 03, 2007
Posts: 14
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
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
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
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

Joined: Jun 04, 2007
Posts: 377
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
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
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
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
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
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
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
Hi Mallik,

you should really read the article I mentioned above.









There are questions you should never answer.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3314

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3314

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
Thanks Everybody especially Raghavan for explaining it.
Mahesh Kamath
Greenhorn

Joined: Mar 02, 2006
Posts: 3
Thanks a lot Manfred.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3314

Of course, ManFred is the * Man NOT to be missed here *!!

Thanks Manfred for the active replies
 
IntelliJ Java IDE
 
subject: How many String objects will be created?
 
Threads others viewed
How many String objects are created?
please provide the explanation regarding string objects............
How many objects ?
How many objects are created?
how many String objects created??
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture