Saturday, November 23, 2019

Difference Betwixt Stringindexoutofboundsexception Together With Arrayindexoutofboundsexception Inwards Java

Though both StringIndexOutOfBoundsException together with ArrayIndexOutOfBoundsException are children of IndexOfBoundsException class, sometime is thrown past times methods of String together with related flat (e.g. StringBuffer together with StringBuilder) to betoken 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 past times grapheme array where index starts at 0 together with ends at length -1. The ArrayIndexOutOfBoundsException is  also quite like to sometime simply thrown past times code which deals alongside array access to betoken that array is accessed past times 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 to a greater extent than or less examples, where JDK throws ArrayIndexOutOfBoundsException together with StringIndexOutOfBoundsException.


Similarities betwixt StringIndexOutOfBoundsException together with ArrayIndexOutOfBounds

  1. Both extends IndexOutOfBoundsException
  2. Both has constructor to practise alongside message together with invalid index
  3. You cannot cast ArrayIndexOutOfBoundsException to StringIndexOutOfBoundsException together with vice-versa.
  4. Throw ArrayIndexOutOfBounds if y'all bargain alongside an array, together with throw StringIndexOutOfBoundsExeption if y'all bargain alongside String.




You tin too come across Core Java Volume i - Fundamentals tenth Edition past times Cay S. Horstmann to acquire to a greater extent than almost these ii exceptions inwards Java.

 sometime is thrown past times methods of String together with related flat  Difference betwixt StringIndexOutOfBoundsException together with ArrayIndexOutOfBoundsException inwards Java



StringIndexOutOfBoundsException vs ArrayIndexOutOfBoundsException Example
Here is a sample Java plan to demonstrate the divergence betwixt these ii exceptions, both are same i.e. they come upwards when y'all endeavour to access the invalid index. You acquire StringIndexOutOfBoundsException when y'all endeavour to access the invalid index of String object using methods of String class.  Similarly, when y'all endeavour to access an invalid index of an array y'all acquire the java.lang.ArrayIndexOutOfBoundsException inwards Java every bit shown inwards the next example.

/**  * Simple Java plan to demonstrate divergence betwixt StringIndexOutOfBoundsException  * vs ArrayIndexOutOfBoundsException  *   * @author WINDOWS 8  *  */ public class TestString {       public static void main(String[] args) {          String gear upwards = "Samsung Galaxy";                  // You acquire StringIndexOutOfBoundsException when y'all 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());                  // simply if endeavour to access invalid index inwards array         // y'all 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: fourteen     at java.lang.String.charAt(String.java:658)     at TestString.main(TestString.java:19) 


That's all almost the divergence betwixt StringIndexOutBoundsException together with ArrayIndexOutOfBoundsException inwards Java. Just think that source travail of both errors is trying to access the invalid index.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

No comments:

Post a Comment