Difference betwixt StringIndexOutOfBoundsException as well as ArrayIndexOutOfBoundsException inwards Java

Though both StringIndexOutOfBoundsException together with ArrayIndexOutOfBoundsException are children of IndexOfBoundsException class, old is thrown yesteryear methods of String together with related shape (e.g. StringBuffer together with StringBuilder) to request that an index is either negative or greater than the size of the String, or to a greater extent than full general non valid. For example, charAt() method throws StringIndexOutOfBoundsException when an index is equal to the length of String? why? because String is backed yesteryear grapheme array where index starts at 0 together with ends at length -1. The ArrayIndexOutOfBoundsException is  also quite like to old only thrown yesteryear code which deals amongst array access to request that array is accessed yesteryear invalid index. The index is either negative or greater than or equal to the length of the array.

In this article, nosotros volition come across around examples, where JDK throws ArrayIndexOutOfBoundsException together with StringIndexOutOfBoundsException.


Similarities betwixt StringIndexOutOfBoundsException together with ArrayIndexOutOfBounds

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



You tin too come across Core Java Volume i - Fundamentals tenth Edition yesteryear Cay S. Horstmann to larn to a greater extent than nigh these 2 exceptions inwards Java.

 old is thrown yesteryear methods of String together with related shape  Difference betwixt StringIndexOutOfBoundsException together with ArrayIndexOutOfBoundsException inwards Java



StringIndexOutOfBoundsException vs ArrayIndexOutOfBoundsException Example
Here is a sample Java programme to demonstrate the divergence betwixt these 2 exceptions, both are same i.e. they come upward when you lot endeavour to access the invalid index. You acquire StringIndexOutOfBoundsException when you lot endeavour to access the invalid index of String object using methods of String class.  Similarly, when you lot endeavour to access an invalid index of an array you lot acquire the java.lang.ArrayIndexOutOfBoundsException inwards Java equally shown inwards the next example.

/**  * Simple Java programme to demonstrate divergence betwixt StringIndexOutOfBoundsException  * vs ArrayIndexOutOfBoundsException  *   * @author WINDOWS 8  *  */ public class TestString {       public static void main(String[] args) {          String ready = "Samsung Galaxy";                  // You acquire StringIndexOutOfBoundsException when you lot try         // to access invalid index of a String inwards Java.         // this volition throw StringIndexOutOfBoundsException         // because final grapheme is at length - 1, non at length         char lastChar = brand.charAt(brand.length());                  // only if endeavour to access invalid index inwards array         // you lot acquire ArrayIndexOutOfBoundsException                  char[] chars = brand.toCharArray();         char firstChar = chars[1]; // invalid index - ArrayIndexOutOfBoundsException              }  }  Output Exception inwards thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: xiv     at java.lang.String.charAt(String.java:658)     at TestString.main(TestString.java:19) 


That's all nigh the divergence betwixt StringIndexOutBoundsException together with ArrayIndexOutOfBoundsException inwards Java. Just recall that rootage drive of both errors is trying to access the invalid index.

Subscribe to receive free email updates:

0 Response to "Difference betwixt StringIndexOutOfBoundsException as well as ArrayIndexOutOfBoundsException inwards Java"

Posting Komentar