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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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:
Original CharBuffer: [a, b, c] value at Index 0 is : c
Example 2: To demonstrate IndexOutOfBoundsException.
Original CharBuffer: [a, b, c] index is greater than the capacity minus 1 Exception throws : java.lang.IndexOutOfBoundsException