aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Chinmay Kant
Greenhorn

Joined: Feb 04, 2008
Posts: 20
--code
------------------------------
public String mkstrings()
{
String s = "fred";
s = s + 47;
s = s.substring(2,5);
s = s.toUppercase();
return s.toString();
}
-------------------------------
How many String Objects are created when the Method is invoked
-----------------------------------------


I think there are 6 Objects created but the Ans says 3


Thanks and Regards,
Chinmay
Chinmay Kant
Greenhorn

Joined: Feb 04, 2008
Posts: 20
Guide Me on How many Objects are created??
Kshitija Mitter
Greenhorn

Joined: Mar 03, 2008
Posts: 11
Hi,

Quite confusing... I too after analysing thought the answer to be 6, Probably I am jumbuled with the basics then..
Daniel Del Moral
Ranch Hand

Joined: May 24, 2008
Posts: 32
I'm guessing that this is a trick question, where you're been tested on knowing the difference between create a String in the string pool and create a String Object.

In your code, you're actually creating three new String Objects on there methods:


And you're creating three more Strings in the String pool:



Although these is a very very tricky queestion, and I don't expected to be in the exam.


SCJP 5, SCWCD 5
John Wenn
Ranch Hand

Joined: Mar 03, 2008
Posts: 31
Here the answer is correct, the reason for that answer is you just assign the String value therefor no new objects were created. if you don't assign the values the answer should be 6.
Marcelo DeOliveira
Greenhorn

Joined: May 08, 2008
Posts: 27
Daniel,

I just want to understand. I thought that only creating a string with NEW would create a object in the heap and one on String Pool! Are you saying then that whenever you run a string method you create a object in the heap?


a SCJP, almost a SCWCD
Daniel Del Moral
Ranch Hand

Joined: May 24, 2008
Posts: 32
Yes. There's a way that you can check what Strings are created in the String Pool and what are created in the Heap.
If two variables have Strings created in the String Pool, then both variables will refer the same object. Check out this code:



This means that s1 and s2 are in the String Pool and S3 and S4 are in the Heap.
Marcelo DeOliveira
Greenhorn

Joined: May 08, 2008
Posts: 27
So, eventhough s is initially (String s = "test") referring to a String in the pool. When you perform:

s = s.substring(2,5); s changes the reference to a new obj now in the in the HEAP?

s=s.toUppercase(); change again?
s=s.toString(); change again?

Waw!!!I didnt know that.
Thanks!
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
I think only 5 objects are created. I read somewhere that toString() on String does not create a new object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings