Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

How to Fix StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java, Example

Though both StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException are children of IndexOfBoundsException class, the former is thrown by methods of String and related class like StringBuffer and StringBuilder to indicate that an index is either negative or greater than the size of the String, or more generally 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 the former but thrown by code which deals with array access to indicate that array is accessed by an 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
Here are some common points between both StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java:
  1. Both extends IndexOutOfBoundsException
  2. Both have constructors 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 Example

Here is a sample Java program to demonstrate the difference between these two exceptions, both are the 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 is 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


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 the root cause of both errors is trying to access the invalid index.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.