CharBuffer charAt() methods in Java with Examples Last Updated : 23 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // 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- Comment More infoAdvertise with us Next Article CharBuffer allocate() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-CharBuffer Java-NIO package Practice Tags : Java Similar Reads CharBuffer allocate() method in Java with Examples The allocate() method of java.nio.CharBuffer Class is used to allocate a new char buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing array 2 min read CharBuffer append() methods in Java with Examples append(char c) The append(char c) method of java.nio.CharBuffer Class is used to append the specified char to this buffer (optional operation). An invocation of this method of the form dst.append(c) behaves in exactly the same way as the invocation dst.put(c) Syntax : public CharBuffer append(char c 6 min read CharBuffer read() methods in Java with Examples The read() method of java.nio.CharBuffer Class is used to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed. Syntax: public int rea 4 min read CharBuffer mark() methods in Java with Examples The mark() method of java.nio.CharBuffer Class is used to set this buffer's mark at its position. Syntax: public CharBuffer mark() Return Value: This method returns this buffer. Below are the examples to illustrate the mark() method: Examples 1: Java // Java program to demonstrate // mark() method i 2 min read CharBuffer flip() methods in Java with Examples The flip() method of java.nio.CharBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of 2 min read CharBuffer chars() methods in Java with Examples The chars() method of java.nio.CharBuffer Class is used to return a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted. The stream binds to this sequence when the terminal stream operation commences (specific 2 min read Like