Demo of ArrayIndexOutOfBoundsException | java.lang package
![]() |
Figure1 |
Class Hierarchy:
ArrayIndexOutOfBoundsException is a class present in java.lang.* package and has following class hierarchy.
Description:
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
JDK1.0
![]() |
Figure2 |
Description:
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
Since:
JDK1.0
Example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.java.lang.exceptions; | |
/** | |
* @author Rohit Agarwal | |
* @version 1.0 | |
* @category java.lang/Exception | |
* @since JDK 1.0 | |
*/ | |
public class ArrayIndexOutOfBoundsExceptionDemo { | |
public static void main(String[] args) { | |
int arr[] = new int[5]; | |
/* | |
* ArrayIndexOutOfBoundException will be thrown because array size is 5 | |
* whose min index is 0 and max index is 4. We are trying to access | |
* element at index 5 that not exists. | |
*/ | |
System.out.println(arr[5]); | |
} | |
} |
Output:
![]() |
Figure3 |
References:
https://docs.oracle.com/javase/8/docs/api/java/lang/ArrayIndexOutOfBoundsException.html
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
Demo of ArrayIndexOutOfBoundsException | java.lang package
Reviewed by Rohit Agarwal
on
10/18/2017
Rating:

Wow! I couldn't even imagine, that it is possible to build so many programs on Java. Your posts will definitely be useful for programmers.
ReplyDelete