title: Why is "out of range" not thrown for 'substring(startIndex, endIndex)'
body: <p>In Java I am using the <code>substring()</code> method and I'm not sure why it is not throwing an "out of index" error.</p>
  
  <p>The string <code>abcde</code> has index start from 0 to 4, but the <code>substring()</code> method takes startIndex and endIndex as arguments based on the fact that I can call foo.substring(0) and get "abcde".</p>
  
  <p>Then why does substring(5) work? That index should be out of range. What is the explanation?</p>
  
  <pre><code>/*
  1234
  abcde
  */
  String foo = "abcde";
  System.out.println(foo.substring(0));
  System.out.println(foo.substring(1));
  System.out.println(foo.substring(2));
  System.out.println(foo.substring(3));
  System.out.println(foo.substring(4));
  System.out.println(foo.substring(5));
  </code></pre>
  
  <p>This code outputs:</p>
  
  <pre><code>abcde
  bcde
  cde
  de
  e
       //foo.substring(5) output nothing here, isn't this out of range?
  </code></pre>
  
  <p>When I replace 5 with 6:</p>
  
  <pre><code>foo.substring(6)
  </code></pre>
  
  <p>Then I get error:</p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
      String index out of range: -1
  </code></pre>