aspose file tools
The moose likes Beginning Java and the fly likes How the subString() function of String class works Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How the subString() function of String class works" Watch "How the subString() function of String class works" New topic
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How the subString() function of String class works
 
Similar Threads
String Objects - Count
Substring method
Another mock doubt
doubt in indexOf()
String methods