Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring method in String class reaches the index it isn't supposed to [duplicate]

I apologize if title is not clear.

Now, in strings index starts at 0. So for instance:

Index    0   1   2   3   4
String   H   E   L   L   O

In this case, the last index is 4.

If I would try to do something like this:

System.out.println("hello".charAt(5));

It would throw an 'Index Out Of Bounds Exception', as it should.

However, if I try to run the following code:

System.out.println("hello".substring(5));

It does run! I wrote similar code, noticed this later and could not figure it out.

Eventually, the question is that how is this possible that we can reach this index we should not? It allows us to write:

stringObj.substring(stringObj.length());

and it doesn't look very safe as stringObj.length() return us an integer that is one more than what the last index is.

Is it made intentionally and does this have a purpose? If so, what is this purpose and what is the benefit it brings?

like image 709
Haggra Avatar asked Nov 09 '15 00:11

Haggra


People also ask

How does substring () inside string works?

The substring(int beginIndex, int endIndex) method of the String class. It 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.

Does substring change the original string Java?

When we take substring from original string, new String object will be created in constant pool or in heap. The value[] char array will be shared among two String objects, but count and offset attributes of String object will vary according to substring length and starting index.

What is the substring method in Java?

What is a Substring in Java? Substring in Java is a commonly used method of java. lang. String class that is used to create smaller strings from the bigger one. As strings are immutable in Java, the original string remains as it is, and the method returns a new string.

How does substring cause memory leak?

In the String object, when you call substring , the value property is shared between the two strings. So, if you get a substring from a big string and keep it for a long time, the big string won't be garbage collected. It could result in a memory leak, actually.


1 Answers

It is easier to work with substring when you think of indexes like this (like letters are between indexes)

         H E L L O
        0 1 2 3 4 5    <- acceptable range of indexes for "HELLO"
        ^         ^
        |         |
      start      end = length
   (min index) (max index)

When you substring from start to end you get only characters between these indexes.

So in case of "HELLO".substring(2,4) you will get part

 L L 
2 3 4

which returns "LL".

Now in case of substring(5) it acts same as substring(5,length) which means in this case it is substring(5,5). So since 5 index exists in our "model" as acceptable value, and since there are no characters between 5 and 5 we are getting empty string as result.

Similar situation happens in case of substring(0,0) substring(1,1) and so on as long as indexes are acceptable by our model.

StringIndexOutOfBoundsException happens only when we try to access indexes which are not acceptable, which means:

  • negative ones: -1, -2, ...
  • greater than length. Here: 6 7, ...
like image 176
Pshemo Avatar answered Oct 29 '22 05:10

Pshemo