Thursday, October 23, 2025
HomeLanguagesJavaCharBuffer charAt() methods in Java with Examples

CharBuffer charAt() methods in Java with Examples

The charAt() method of java.nio.CharBuffer Class is used to read the character at the given index relative to the current position.

Syntax:

public final char charAt(int index)

Parameters: This method takes the index of the character to be read, relative to the position; must be non-negative and smaller than remaining().

Return Value: This method returns the character at index position() + index.

Exception: This method throws IndexOutOfBoundsException if the preconditions on index do not hold.

Below are the examples to illustrate the charAt(int index) method:

Example 1:




// Java program to demonstrate
// charAt() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating the CharBuffer
        try {
  
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(3);
  
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c')
                .rewind();
  
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
  
            // Read char at particular Index
            // using charAt() method
            char value = charbuffer.charAt(2);
  
            // Display the value
            System.out.println("\nvalue at Index 0 is : "
                               + value);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nindex is greater than"
                               + " the capacity minus 1");
            System.out.println("Exception throws : " + e);
        }
    }
}


Output:

Original CharBuffer:  [a, b, c]

value at Index 0 is : c

Example 2: To demonstrate IndexOutOfBoundsException.




// Java program to demonstrate
// charAt() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating the CharBuffer
        try {
  
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(3);
  
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c')
                .rewind();
  
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
  
            // Read char at particular Index
            // using charAt() method
            char value = charbuffer.charAt(3);
  
            // Display the value
            System.out.println("\nvalue at Index 0 is : "
                               + value);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nindex is greater than"
                               + " the capacity minus 1");
            System.out.println("Exception throws : " + e);
        }
    }
}


Output:

Original CharBuffer:  [a, b, c]

index is greater than the capacity minus 1
Exception throws : java.lang.IndexOutOfBoundsException

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#charAt-int-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS