Difference between StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java

Though both StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException are children of IndexOfBoundsException class, former is thrown by methods of String and related class (e.g. StringBuffer and StringBuilder) to indicate that an index is either negative or greater than the size of the String, or more general not valid. For example, charAt() method throws StringIndexOutOfBoundsException when an index is equal to the length of String? why? because String is backed by character array where index starts at 0 and ends at length -1. The ArrayIndexOutOfBoundsException is  also quite similar to former but thrown by code which deals with array access to indicate that array is accessed by invalid index. The index is either negative or greater than or equal to the length of the array.

In this article, we will see some examples, where JDK throws ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException.


Similarities between StringIndexOutOfBoundsException and ArrayIndexOutOfBounds

  1. Both extends IndexOutOfBoundsException
  2. Both has constructor to create with message and invalid index
  3. You cannot cast ArrayIndexOutOfBoundsException to StringIndexOutOfBoundsException and vice-versa.
  4. Throw ArrayIndexOutOfBounds if you deal with an array, and throw StringIndexOutOfBoundsExeption if you deal with String.



You can also see Core Java Volume 1 - Fundamentals 10th Edition by Cay S. Horstmann to learn more about these two exceptions in Java.

StringIndexOutOfBoundsException vs ArrayIndexOutOfBoundsException



StringIndexOutOfBoundsException vs ArrayIndexOutOfBoundsException Example
Here is a sample Java program to demonstrate the difference between these two exceptions, both are same i.e. they come when you try to access the invalid index. You get StringIndexOutOfBoundsException when you try to access the invalid index of String object using methods of String class.  Similarly, when you try to access an invalid index of an array you get the java.lang.ArrayIndexOutOfBoundsException in Java as shown in the following example.

/**
 * Simple Java program to demonstrate difference between StringIndexOutOfBoundsException
 * vs ArrayIndexOutOfBoundsException
 * 
 * @author WINDOWS 8
 *
 */
public class TestString {


    public static void main(String[] args) {

        String brand = "Samsung Galaxy";
        
        // You get StringIndexOutOfBoundsException when you try
        // to access invalid index of a String in Java.
        // this will throw StringIndexOutOfBoundsException
        // because last character is at length - 1, not at length
        char lastChar = brand.charAt(brand.length());
        
        // but if try to access invalid index in array
        // you get ArrayIndexOutOfBoundsException
        
        char[] chars = brand.toCharArray();
        char firstChar = chars[1]; // invalid index - ArrayIndexOutOfBoundsException
        
    }

}

Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14
    at java.lang.String.charAt(String.java:658)
    at TestString.main(TestString.java:19)


That's all about the difference between StringIndexOutBoundsException and ArrayIndexOutOfBoundsException in Java. Just remember that root cause of both errors is trying to access the invalid index.


No comments:

Post a Comment