Why doesn't following java code throw java.lang.StringIndexOutOfBoundsException when there is no element present at index 1?

Sandeep D
String str="x";
System.out.println(str.substring(1));

From Javadoc:

String java.lang.String.substring(int beginIndex): Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Glorfindel

Take a look at the JavaDoc, and pay specific attention to the "emptiness" example:

Returns a string that is a substring of this string. The substring begins
with the character at the specified index and extends to the end of
this string. 

Examples: 

 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)

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

An exception is throw if beginIndex is negative or larger than the length of this String object; in your case, beginIndex is equal to the length of your string, and not larger.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why doesn't following java code throw java.lang.StringIndexOutOfBoundsException when there is no element present at index 1?

From Dev

Java.lang.StringIndexOutOfBoundsException

From Dev

java.lang.StringIndexOutOfBoundsException: String index out of range: 12 in java

From Dev

java.lang.StringIndexOutOfBoundsException: String index out of range: 12 in java

From Dev

Java - java.lang.StringIndexOutOfBoundsException: String index out of range: 11

From Dev

curl errors: could not resolve host, java.lang.StringIndexOutOfBoundsException: String index out of range: -1

From Dev

Runtime Error for an anagram - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

From Dev

Rebalance topology throws java.lang.StringIndexOutOfBoundsException: String index out of range: -1

From Dev

Runtime Error for an anagram - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

From Dev

Why am I getting a java.lang.StringIndexOutOfBoundsException error when I run?

From Dev

"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range"

From Dev

"main" java.lang.StringIndexOutOfBoundsException: String index out of range: 17

From Dev

java.lang.StringIndexOutOfBoundsException: String index out of range: 4

From Dev

java.lang.StringIndexOutOfBoundsException: String index out of range: (again)

From Dev

java.lang.StringIndexOutOfBoundsException: String index out of range: -23 error

From Dev

Why doesn't for-each method in java not throw an exception when a Function type argument is passed instead of Consumer?

From Dev

Why Java code doesn't work when moved into a function?

From Dev

Getting java.lang.StringIndexOutOfBoundsException

From Dev

Why is there an error in the following java code?

From Dev

Why doesn't this code throw a ConcurrentModificationException?

From Dev

Why doesn't this ruby code throw an exception?

From Dev

why doesn't the java compiler rewrite this code?

From Dev

The following code doesn't work .. why?

From Dev

Why following code doesn't compile?

From Dev

why the following code doesn't perform as expected?

From Dev

The following code doesn't work .. why?

From Dev

Why following code doesn't compile?

From Dev

Why Java throw java.lang.IllegalMonitorStateException when I invoke wait() in static way synchronized block?

From Dev

Appliction throw java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 after deleting item

Related Related

  1. 1

    Why doesn't following java code throw java.lang.StringIndexOutOfBoundsException when there is no element present at index 1?

  2. 2

    Java.lang.StringIndexOutOfBoundsException

  3. 3

    java.lang.StringIndexOutOfBoundsException: String index out of range: 12 in java

  4. 4

    java.lang.StringIndexOutOfBoundsException: String index out of range: 12 in java

  5. 5

    Java - java.lang.StringIndexOutOfBoundsException: String index out of range: 11

  6. 6

    curl errors: could not resolve host, java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  7. 7

    Runtime Error for an anagram - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  8. 8

    Rebalance topology throws java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  9. 9

    Runtime Error for an anagram - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  10. 10

    Why am I getting a java.lang.StringIndexOutOfBoundsException error when I run?

  11. 11

    "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range"

  12. 12

    "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 17

  13. 13

    java.lang.StringIndexOutOfBoundsException: String index out of range: 4

  14. 14

    java.lang.StringIndexOutOfBoundsException: String index out of range: (again)

  15. 15

    java.lang.StringIndexOutOfBoundsException: String index out of range: -23 error

  16. 16

    Why doesn't for-each method in java not throw an exception when a Function type argument is passed instead of Consumer?

  17. 17

    Why Java code doesn't work when moved into a function?

  18. 18

    Getting java.lang.StringIndexOutOfBoundsException

  19. 19

    Why is there an error in the following java code?

  20. 20

    Why doesn't this code throw a ConcurrentModificationException?

  21. 21

    Why doesn't this ruby code throw an exception?

  22. 22

    why doesn't the java compiler rewrite this code?

  23. 23

    The following code doesn't work .. why?

  24. 24

    Why following code doesn't compile?

  25. 25

    why the following code doesn't perform as expected?

  26. 26

    The following code doesn't work .. why?

  27. 27

    Why following code doesn't compile?

  28. 28

    Why Java throw java.lang.IllegalMonitorStateException when I invoke wait() in static way synchronized block?

  29. 29

    Appliction throw java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 after deleting item

HotTag

Archive