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:
Java Vector elementAt() Method
Next article icon

Java String codePointAt() Method

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The codePointAt() method in Java is part of the String class. It is used to return the Unicode value of the character at the specified index in the string. This method is very useful when working with characters beyond the Basic Multilingual Plane (BMP), such as emojis or special symbols.

Example 1: The below Java program demonstrates how to retrieve the Unicode value of a character at the specified index.

Java
// Java Program to demonstrate  // the working of codePointAt() method public class Geeks {     public static void main(String[] args)     {         String s = "Hello";          // Get the Unicode code point of          // the character at index 1         int a = s.codePointAt(1);         System.out.println(             "Unicode value of character at index 1: "             + a);     } } 

Output
Unicode value of character at index 1: 101 

Syntax of codePointAt() Method

public int codePointAt(int index)

  • Parameter: The index of the character to retrieve the Unicode code point. It must be within the range 0 to string length - 1.
  • Return Type: Return an int value representing the Unicode value at the specified index.

Example 2: The below Java program demonstrates how to retrieve the Unicode value of a character including an emoji, at a specified index in a string.

Java
// Java program to demonstrate how to   // retrieve the Unicode value of an emoji import java.io.*;  class Geeks {     public static void main(String[] args)     {          // String with Unicode Created         String s = "Geeks for 🤖 Geeks";          // Get the Unicode code point of the         //  emoji at index 5         int a = s.codePointAt(10);          System.out.println("Code point at index 10 : " + a);     } } 

Output
Code point at index 10 : 129302 

Example 3: If we attempt to access an invalid index, the codePointAt() method will throw an IndexOutOfBoundsException.

Java
// Java program to demonstrate IndexOutOfBoundsException import java.io.*;  public class Geeks {     public static void main(String[] args)     {         String s = "Geeks";          // Trying to access an invalid index         // This will throw IndexOutOfBoundsException              int a = s.codePointAt(10);         System.out.println("Unicode code point: " + a);     } } 

Output:

Output

Note: The exception occurs because the index 10 is beyond the string length (which is 5 in this case).

Example 4: To handle the exception correctly, we can use a try-catch block as shown in the below example:

Java
// Java program to demonstrate // exception handling with codePointAt() import java.io.*;  public class Geeks {     public static void main(String[] args)     {         String s = "Geeks";          try {                        // Trying to access an invalid index             int a = s.codePointAt(10);              // This will throw IndexOutOfBoundsException             System.out.println("Unicode code point: " + a);         }         catch (IndexOutOfBoundsException e) {              // Handling the exception             System.out.println("Error: " + e.getMessage());         }     } } 

Output
Error: index 10,length 5 

Next Article
Java Vector elementAt() Method

M

musklf2s
Improve
Article Tags :
  • Java
  • Java-Strings
Practice Tags :
  • Java
  • Java-Strings

Similar Reads

  • Java String codePoint() Method with Examples
    A Java string consists of a group of characters and each character is associated with a Unicode point value (alias ASCII value). So to get the Unicode point value of a character in a string we will use the codepoint() method. So in order to move further, we need to know what are the associated Unico
    4 min read
  • Java String charAt() Method
    String charAt() method in Java returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on. The index value should lie between 0 and length() - 1. If the index value is greater than or equal to the string length or
    3 min read
  • Java String indexOf() Method
    In Java, the String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. In this article, we will learn various ways to use indexOf() in Java. Example: Below is the simplest way that returns the index of the first occurrence of a s
    3 min read
  • StringBuffer codePointAt() method in Java with Examples
    The codePointAt() method of StringBuffer class returns a character Unicode point at that index in sequence contained by StringBuffer. This method returns the “Unicodenumber” of the character at that index. Value of index must be lie between 0 to length-1. If the char value present at the given index
    2 min read
  • Java Vector elementAt() Method
    In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based. Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Intege
    2 min read
  • StringBuffer codePointCount() method in Java with Examples
    The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of
    3 min read
  • StringBuffer codePointBefore() method in Java with Examples
    The codePointBefore() method of StringBuffer class is a method used to take an index as a parameter and returns the “Unicode number” of the character present before that index. The value of index must lie between 0 to length-1. If the char value at (index – 1) is in the low-surrogate range, char at
    2 min read
  • StringBuffer appendCodePoint() Method in Java with Examples
    appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an in
    3 min read
  • Array setInt() method in Java
    The java.lang.reflect.Array.setInt() is an inbuilt method in Java and is used to set a specified int value to a specified index of a given object array. Syntax: Array.setInt(Object []array, int index, int value) Parameter: array: This is an array of type Object which is to be updated. index: This is
    3 min read
  • StringBuilder appendCodePoint() method in Java with Examples
    The appendCodePoint(int codePoint) method of StringBuilder class is the inbuilt method used to append the string representation of the codePoint argument to this sequence. The argument is appended to this StringBuilder content and length of the object is increased by Character.charCount(codePoint).
    2 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