Last item of an array in JAVA

Get last item of an array in JAVA

To get the last item of an array in java we get the length of the array and subtract one because the indexes of the array start from 0 it means if the size of the array is 4 then the last item index is 3.

public class LastItemOfArray {

    public static void main(String args[]) {
        Integer[] array = {1, 2, 3, 4, 5};
        System.out.println(array[array.length-1]);
        // Output: 5
    }

}