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:
Array getShort() method in Java
Next article icon

Array get() Method in Java

Last Updated : 23 Jul, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax
  Array.get(Object []array, int index)  
Parameters : This method accepts two mandatory parameters:
  • array: The object array whose index is to be returned.
  • index: The particular index of the given array. The element at 'index' in the given array is returned.
Return Value: This method returns the element of the array as type of Object class. Exceptions: This method throws following exceptions:
  • NullPointerException - when the array is null.
  • IllegalArgumentException - when the given object array is not an Array.
  • ArrayIndexOutOfBoundsException - if the given index is not in the range of the size of the array.
Below programs illustrate the get() method of Array class: Program 1: Java
import java.lang.reflect.Array;  public class GfG {     // main method     public static void main(String[] args)     {         // Declaring and defining an int array         int a[] = { 1, 2, 3, 4, 5 };          // Traversing the array         for (int i = 0; i < 5; i++) {              // Array.get method             // Note : typecasting is essential             // as the return type in Object.             int x = (int)Array.get(a, i);              // Printing the values             System.out.print(x + " ");         }     } } 
Output:
  1 2 3 4 5  
Program 2: To demonstrate ArrayIndexOutOfBoundsException. Java
import java.lang.reflect.Array;  public class GfG {     // main method     public static void main(String[] args)     {         // Declaring and defining an int array         int a[] = { 1, 2, 3, 4, 5 };          try {             // invalid index             int x = (int)Array.get(a, 6);             System.out.println(x);         }         catch (Exception e) {             // throws Exception             System.out.println("Exception : " + e);         }     } } 
Output:
  Exception : java.lang.ArrayIndexOutOfBoundsException  
Program 3: To demonstrate NullPointerException. Java
import java.lang.reflect.Array;  public class GfG {     // main method     public static void main(String[] args)     {         // Declaring an int array         int a[];          // array to null         a = null;          try {             // null Object array             int x = (int)Array.get(a, 6);             System.out.println(x);         }         catch (Exception e) {             // throws Exception             System.out.println("Exception : " + e);         }     } } 
Output:
  Exception : java.lang.NullPointerException  
Program 4: To demonstrate IllegalArgumentException. Java
import java.lang.reflect.Array;  public class GfG {     // main method     public static void main(String[] args)     {         // int (Not an array)         int y = 0;          try {             // illegalArgument             int x = (int)Array.get(y, 6);              System.out.println(x);         }         catch (Exception e) {             // Throws exception             System.out.println("Exception : " + e);         }     } } 
Output:
  Exception : java.lang.IllegalArgumentException: Argument is not an array  

Next Article
Array getShort() method in Java

S

ShivamKD
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-Arrays
  • Java-Functions
  • java-reflection-array
  • java-lang-reflect-package
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • 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
  • Array getByte() Method in Java
    The java.lang.reflect.Array.getByte() is an inbuilt method in Java and is used to return the element present at the given index from the specified Array as a Byte.Syntax: Array.getByte(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose
    3 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 getChar() Method in Java
    The java.lang.reflect.Array.getChar() is an inbuilt method in Java and is used to return the element present at a given index from the specified Array as a char. Syntax Array.getChar(Object []array,int index) Parameters : array : The object array whose index is to be returned.index : The particular
    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
  • Array getFloat() Method in Java
    The java.lang.reflect.Array.getFloat() is an inbuilt method of Array class in Java and is used to return the element present at the given index from the specified Array as Float. Syntax: Array.getFloat(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The ob
    3 min read
  • Array getDouble() Method in Java
    The java.lang.reflect.Array.getDouble() is an inbuilt method of Array class in Java and is used to return the element present at the given index from the specified Array as Double. Syntax: Array.getDouble(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The
    3 min read
  • Array getBoolean() Method in Java
    The java.lang.reflect.Array.getBoolean() returns the given index from the specified Array as a short. Syntax: Array.getBoolean(Object []array,int index) Parameters: array: The object array whose index is to be returned. index: The particular index of the given array. The element at 'index' in the gi
    2 min read
  • ArrayDeque getLast() Method in Java
    The java.util.ArrayDeque.getLast() method in Java is used to retrieve or fetch the last element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the last element of the deque.Syntax: Array_Deque.getLast() Parameters: The method does not
    2 min read
  • Array set() method in Java
    The java.lang.reflect.Array.set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array. Syntax Array.set(Object []array, int index, Object value) Parameter : array : This is an array of type Object which is to be updated. index : This is the
    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