• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does substring method works?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That blog is suggesting bad programming practice, viz inspecting the implementation rather than the public interface.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very welcome!

And welcome to the Ranch!
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic