• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Chinmay Kant
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--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
 
Chinmay Kant
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guide Me on How many Objects are created??
 
Kshitija Mitter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Wenn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Daniel Del Moral
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think only 5 objects are created. I read somewhere that toString() on String does not create a new object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic