File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 19649
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: How the subString() function of String class works
 
Similar Threads
String methods
Another mock doubt
doubt in indexOf()
Substring method
String Objects - Count