wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How does substring method works? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does substring method works?" Watch "How does substring method works?" New topic
Author

How does substring method works?

Jyothirmai Vissapragada
Greenhorn

Joined: Sep 22, 2006
Posts: 4
Hi,

I came across a nice post on http://javarevisited.blogspot.com/ about "How subString method works in Java". here is the link http://javarevisited.blogspot.com/2011/10/how-substring-in-java-works.html

But, I could not understand what does "Most of the people fail here because they don't know how exactly it works until they have not seen the code of java.lang.String. If you look substring method inside String class you will figure out that it calls String (int offset, int count, char value []) constructor to create new String object what is interesting here is value[] which is the same character array used to represent original string. " mean. I tried to look for the original String class subString() code but i could not get it. Can someone pleae explain me this in more detail?

Thank you in advance.

-Jyothi
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

First this topic has nothing to do with Threads and Synchronization. It would have been better placed in Beginning Java. In the future, please CarefullyChooseOneForum (<-- link).

As to your question, a String object has something the following:


Now, when you construct a brand new String that's not part of some other String, offset will be 0 and int will be equal to value.length. That means that this String uses the entire array.



So this String uses character 0 through character 4 (5 characters) in the array {'a', 'b', 'c', 'd', 'e'}

If we do


In this case, s2 has a reference to the same char[] as s1 (so that we can avoid the cost of creating a new array and copying characters). S2's offset is 1, meaning it starts at index 1, which is where 'b' is, and the count is 2, meaning it uses 2 characters, so s2 is "bc"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
That blog is suggesting bad programming practice, viz inspecting the implementation rather than the public interface.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:That blog is suggesting bad programming practice, viz inspecting the implementation rather than the public interface.


Good point. We shouldn't have to care. However, in this particular case, it can be worth knowing these details, though the actual use case is quite rare.

If you take a small substring of a large String, and that substring lives on long after you're done with the larger String, you can end up wasting memory. I thought that substring()'s docs used to even mention it, but maybe I first saw it somewhere else. I've never actually run across this in a dozen or so years of Java programming, but if it did happen, and you didn't know about this particular String implementation detail, you could be scratching your head for a while trying to figure out why you're running out of memory.

The simple work around is to do


rather than



because then the new String will get its own backing array.

However, this is a pointless premature optimization in the hugely vast majority of cases. Only when you know for certain--or better yet have measured with a profiler--that you'll be wasting significant memory should you do this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
substring

public String substring(int beginIndex,
int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"


Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
Returns:
the specified substring.
Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

Nothing about backing arrays there, and that is from Java1.2 API. I found a β version of the Java1.1 API, which has this
substring

public String substring(int beginIndex,
int endIndex)

Returns the substring of a String. The substring is specified by a beginIndex (inclusive) and an endIndex (exclusive).

Parameters:
beginIndex - the beginning index, inclusive
endIndex - the ending index, exclusive
Throws: StringIndexOutOfBoundsException
If the beginIndex or the endIndex is out of range, or if the beginIndex is greater than the endIndex..

It might have been useful to warn about the potential memory problem, particularly in the early days when memory was more expensive.
Jyothirmai Vissapragada
Greenhorn

Joined: Sep 22, 2006
Posts: 4
Jeff Verdegan wrote:First this topic has nothing to do with Threads and Synchronization. It would have been better placed in Beginning Java. In the future, please CarefullyChooseOneForum (<-- link).

As to your question, a String object has something the following:


Now, when you construct a brand new String that's not part of some other String, offset will be 0 and int will be equal to value.length. That means that this String uses the entire array.



So this String uses character 0 through character 4 (5 characters) in the array {'a', 'b', 'c', 'd', 'e'}

If we do


In this case, s2 has a reference to the same char[] as s1 (so that we can avoid the cost of creating a new array and copying characters). S2's offset is 1, meaning it starts at index 1, which is where 'b' is, and the count is 2, meaning it uses 2 characters, so s2 is "bc"



Sorry for posting my question under a different thread. I will be careful from the next time.

The explanation was very clear and helpful. Thank you very much for your time and for the immediate response.

-Jyothi
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're very welcome!

And welcome to the Ranch!
 
wood burning stoves
 
subject: How does substring method works?
 
Similar Threads
Screen Scrapping Problem!
when to use string literal and String New Object ?
trim() method
StringIndexOutOfBoundsException
substring question