OneBite.Dev - Coding blog in a bite size

get the first element of array in java

Code snippet on how to get the first element of array in java

  // Assume the array is called arrayName
  Object firstElement = arrayName[0];

This code retrieves the first element in an array. First, we assumed that the array is called arrayName. Then we declared a new object named firstElement and set it to the first element in the arrayName. All arrays in Java start the index at 0, so in order to grab the first element, we use the index “0”. The first element of the array is now stored in the firstElement object. This code can be used to get the first element of any array in Java.

java