Initialize arrat and print length
public class Main {
public static void main(String[] args) {
int[] arr = {10, 21, 3};
System.out.println(arr.length);
}
}3
Access element at specific index
public class Main2 {
public static void main(String[] args) {
int[] arr = {10, 21, 3};
// Print element at index 1
// index starts from 0
System.out.println(arr[1]);
}
}21
Access non existing index
This will throw Index Out Of Bounds Exception
public class Main3 {
public static void main(String[] args) {
int[] arr = {10, 21, 3};
// index out of bound
System.out.println(arr[10]);
}
}Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Main3.main(Main3.java:5)