How to Get First Element in Array in Java? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 6 Likes Like Report In Java, to get the first element in an array, we can access the element at index 0 using array indexing. Example 1: Below is a simple example that demonstrates how to access the element at index 0 in an array. Java public class Geeks { public static void main(String[] args) { // Declare and initialize an array int[] n = {5, 15, 25, 35, 45}; // Access the first element int f = n[0]; // Print the first element System.out.println("" + f); } } Output5 SyntaxarrayName[0];arrayName: The name of the array.[0]: The index of the first element in the array.Example 2: Here, we are trying to access the first element in an empty array. It will throw an ArrayIndexOutOfBoundsException. Java // Handling empty arrays public class Geeks { public static void main(String[] args) { // Declare an empty array int[] n = {}; // Check if the array is empty if (n.length > 0) { System.out.println("The first element is: " + n[0]); } else { System.out.println("The array is empty."); } } } OutputThe array is empty. Example 3: Here, we are accessing the first element in an Array of String. Java // Access the first element // in a String array public class Geeks { public static void main(String[] args) { // Declare and initialize a string array String[] s = {"Cherry", "Strawberry", "Blueberry"}; // Access the first element String f = s[0]; // Print the first element System.out.println(" " + f); } } Output Cherry Create Quiz Comment S shubhamkquv4 Follow 6 Improve S shubhamkquv4 Follow 6 Improve Article Tags : Java Java-Arrays Java-Array-Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like