VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-get-last-element-in-array-in-java/

⇱ How to Get Last Element in Array in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Get Last Element in Array in Java?

Last Updated : 23 Jul, 2025

In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing. The length property of the array provides its total size, and subtracting one from it gives the index of the last element.

Example 1: Here, we will access the last element in an Integer array.


Output
5

Syntax

arrayName[arrayName.length - 1];

  • arrayName: The name of the array.
  • length: The total number of elements in the array.
  • - 1: Subtracting one gives the index of the last element.

Example 2: Here, we will access the last element in an array of Strings.


Output
Blueberry


Example 3: If we try to access the last element of an empty array, it will throw an ArrayIndexOutOfBoundsException.


Output
The array is empty.
Comment
Article Tags: