Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4125144

StringIndexOutOfBoundsException comments too strong about index values.substring

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 1.4.0
    • 1.1.5, 1.2.0
    • docs
    • beta
    • x86
    • windows_nt



      Name: tb29552 Date: 04/02/98


      The JavaDoc for StringIndexOutOfBoundsException
      (http://java.sun.com/products/jdk/1.1/docs/api/java.lang.StringIndexOutOfBoundsException.html#_top_)
      states:

           Thrown by the charAt method in class String and by other String methods to
           indicate that an index is either negative or greater than or equal
           to the size of the string.

      Yet the following does _not_ throw that exception:


      class XY {
          public static void outputMessage(){
      System.out.println("java.version = " +
      System.getProperty("java.version"));
      System.out.println("O/S Name = " +
      System.getProperty("os.name"));
      System.out.println("O/S architecture = " +
      System.getProperty("os.arch"));
      System.out.println("O/S version = " +
      System.getProperty("os.version"));
          }

          public static void main(String[] args){
      outputMessage();

      String x = "X";
      String y = x.substring( x.length() );
      System.out.println( "x is of length " + x.length( ) );
      System.out.println( "and y (" + y + ") is of length " + y.length( ) );

      String z = "mumble";
      String w = z.substring( z.length() );
      System.out.println( "z is of length " + z.length( ) );
      System.out.println( "and w (" + w + ") is of length " + w.length( ) );

          }
      }

      Clearly, the ``index is ... equal to the size of the String.'' The above code prints:

        % java XY
      java.version = 1.2beta3
      O/S Name = SunOS
      O/S architecture = sparc
      O/S version = 5.6
      x is of length 1
      and y () is of length 0
      z is of length 6
      and w () is of length 0

      Is this an implementation bug or a doc bug?

      (Review ID: 27677)
      ======================================================================

            jhosunw Jamie Ho (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: