| Author |
How the subString() function of String class works
|
Harshit Rastogi
Ranch Hand
Joined: Apr 15, 2008
Posts: 131
|
|
Hi
please see the following code.
I know that line 3 will still point to "Monday" and have a new String object with the offset and count set to 0,3.
The line 5 will create a new String "Mon" in string pool and point to it.
But not sure what about line 6 whether it will behave like line 3 or line 5.
If i am wrong for line 2 or 4 also please correct..
Thanks
|
<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
|
 |
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
|
|
|
Check out the source of substring() method. Then you will find out that line 5 is instantiating the new String object redundantly.
|
 |
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
|
|
|
This JR Thread might be helpful.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
substring always creates a new String object, except when the substring would actually be the String itself (i.e. the start is 0 and the end is equal to the String length).
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: How the subString() function of String class works
|
|
|