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
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


My Techie Blog - Java Questions
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: 19760
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How the subString() function of String class works