Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ArrayDeque getFirst() Method in Java
Next article icon

How to Get First Element in Array in Java?

Last Updated : 24 Dec, 2024
Comments
Improve
Suggest changes
Like Article
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);     } } 

Output
5 

Syntax

arrayName[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.");         }     } } 

Output
The 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 

Next Article
ArrayDeque getFirst() Method in Java

S

shubhamkquv4
Improve
Article Tags :
  • Java
  • Java-Arrays
  • Java-Array-Programs
Practice Tags :
  • Java

Similar Reads

  • How to Get Last Element in Array in Java?
    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 a
    2 min read
  • Get first and last elements from ArrayList in Java
    Given an array list, find the first and last elements of it. Examples: Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60 The last element is at index, size - 1 and the first element is stored at index 0. If we know h
    3 min read
  • How to Declare an Array in Java?
    In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif
    3 min read
  • How to Check whether Element Exists in Java ArrayList?
    Java ArrayList is a resizable array, which can be found in java.util package. We can add or delete elements from an ArrayList whenever we want, unlike a built-in array. We can check whether an element exists in ArrayList in java in two ways: Using contains() methodUsing indexOf() method Method 1: Us
    2 min read
  • ArrayDeque getFirst() Method in Java
    The java.util.ArrayDeque.getFirst() method in Java is used to retrieve or fetch the first element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the first element of the deque. Syntax: Array_Deque.getFirst() Parameters: The method doe
    2 min read
  • Find first and last element of ArrayList in java
    Prerequisite: ArrayList in Java Given an ArrayList, the task is to get the first and last element of the ArrayList in Java, Examples: Input: ArrayList = [1, 2, 3, 4] Output: First = 1, Last = 4 Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: First = 12, Last = 89 Approach: Get the ArrayList
    2 min read
  • Array getInt() Method in Java
    The java.lang.reflect.Array.getInt() is an inbuilt method in Java and is used to return an element at the given index from the specified Array as a int. Syntax Array.getInt(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The object array whose index is to
    3 min read
  • ArrayDeque element() Method in Java
    The java.util.ArrayDeque.element() method in Java is used to retrieve or fetch the head of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the element. Syntax: Array_Deque.element() Parameters: The method does not take any parameter. Retu
    2 min read
  • Array getLong() Method in Java
    The java.lang.reflect.Array.getLong() is an inbuilt method in Java and is used to return an element at the given index from a specified Array as a long. Syntax: Array.getLong(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is
    3 min read
  • Array getShort() method in Java
    The java.lang.reflect.Array.getShort() is an in-built method of Array class in Java and is used to return the element present at a given index from the specified Array as a short. Syntax: Array.getShort(Object []array,int index) Parameters: array: The object array whose index is to be returned. inde
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences