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 getChar() Method in Java

Last Updated : 23 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 index of the given array. The element at 'index' in the given array is returned.

Return Type: This method returns the element of the array as char. Note: Typecast isn't necessary as the return type is char Exception: This method throws following exception

  • 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 getChar() method of Array Class: Program 1: 

Java
// Java code to demonstrate getChar() method of Array class import java.lang.reflect.Array; public class GfG  {     // main method     public static void main(String[] args) {                 // Declaring and defining an byte array        char a[] = {'G','f','G'};                // Traversing the array        for(int i = 0;i<3;i++){                        // Array.getChar() method                        char x = Array.getChar(a, i);            // Printing the values            System.out.print(x);        }              } } 
Output:
GfG

Program 2: 

Java
// Java code to demonstrate getChar() method in Array import java.lang.reflect.Array;   public class GfG {       // main method     public static void main(String[] args) {         // Declaring and defining an char array         char a[] = {'G','f','G'};           try {             // invalid index             char x =  Array.getChar(a, 6);             System.out.println(x);         } catch (Exception e) {             // throws Exception             System.out.println("Exception : " + e);         }     } } 
Output:
Exception : java.lang.ArrayIndexOutOfBoundsException

Program 3: 

Java
// Java code to demonstrate getChar() method in Array import java.lang.reflect.Array;   public class GfG {       // main method     public static void main(String[] args) {         // Declaring and defining an char array to null         char a[] = null;           try {             // null Object array             char x =  Array.getChar(a, 6);             System.out.println(x);         } catch (Exception e) {             // throws Exception             System.out.println("Exception : " + e);         }     } } 
Output:
Exception : java.lang.NullPointerException

Program 4: 

Java
// Java code to demonstrate getChar() method in Array import java.lang.reflect.Array;   public class GfG {       // main method     public static void main(String[] args) {         // Declaring and defining an char variable         char a = 'a';           try {             //illegal Argument             char x =  Array.getChar(a, 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 :
  • Misc
  • Java
  • Java-Collections
  • Java-Arrays
  • Java-Functions
  • java-reflection-array
  • java-lang-reflect-package
Practice Tags :
  • Java
  • Java-Collections
  • Misc

Similar Reads

  • Array get() Method in Java
    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. in
    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 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
  • CharBuffer array() method in Java
    The array() method of java.nio.CharBuffer Class is used to return the char array that backs this buffer. Any modifications done to this buffer's content will cause the returned array's content also to be modified, and vice versa. hasArray() method is invoked before invoking this method in order to e
    3 min read
  • CharBuffer arrayOffset() method in Java
    The arrayOffset() method of java.nio.CharBuffer class is used to return the offset within the buffer's backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). Inorder to check whether t
    3 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
  • CharBuffer get() methods in Java
    The get() method of java.nio.CharBuffer Class is used to reads the char at the given buffer's current position, and then increments the position. Syntax: public abstract char get() Return Value: This method returns the char value at the buffer's current position. Exceptions: This method throws Buffe
    5 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
  • ArrayDeque add() Method in Java
    The Java.util.ArrayDeque.add(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the addLast() method of ArrayDeque in Java. Syntax: Array_Deque.add(Object element) Parameters: The parameter element is of the type ArrayDeque and refers
    2 min read
  • IntBuffer array() method in Java
    The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure
    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