Java StringBuffer charAt() Method Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The charAt() method in the StringBuffer class in Java is used to retrieve the character at a given index in a StringBuffer object. This method allows us to access individual characters in a StringBuffer by specifying the index.Example 1: Here, we will use a valid index to retrieve a character from the StringBuffer object. Java // Java program to demonstrate charAt() method public class CharAt { public static void main(String[] args) { StringBuffer b = new StringBuffer("Java Programming"); // Using charAt() to get the // character at index 5 char ch = b.charAt(5); System.out.println("" + ch); } } OutputP Syntax of charAt() Methodpublic char charAt(int index)Parameters: index: This is an integer representing the index of the character to be fetched.Return Type: char: The character at the specified index.Example 2: If we use charAt() with a Negative Index a StringIndexOutOfBoundsException is thrown, as negative indices are not allowed. Java // Java program to demonstrate // invalid negative index in charAt() public class CharAt { public static void main(String[] args) { StringBuffer b = new StringBuffer("Java Programming"); try { // Using a negative index System.out.println("Character at index -1: " + b.charAt(-1)); } catch (StringIndexOutOfBoundsException e) { System.out.println("Exception: " + e); } } } OutputException: java.lang.StringIndexOutOfBoundsException: index -1,length 16 Example 3: If we pass an index greater than or equal to the length of the StringBuffer object will also result in a StringIndexOutOfBoundsException. Java // Java program to demonstrate invalid index // greater than length in charAt() public class CharAt { public static void main(String[] args) { StringBuffer b = new StringBuffer("Java Programming"); try { // Using an index greater than the length of the buffer System.out.println("Character at index 20: " + b.charAt(20)); } catch (StringIndexOutOfBoundsException e) { System.out.println("Exception: " + e); } } } OutputException: java.lang.StringIndexOutOfBoundsException: index 20,length 16 Comment More infoAdvertise with us Next Article StringBuffer Class in Java J juhisrivastav Follow Improve Article Tags : Java Java-Strings java-StringBuffer Practice Tags : JavaJava-Strings Similar Reads StringBuffer deleteCharAt() Method in Java with Examples The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of 2 min read StringBuffer getChars() method in Java with Examples The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuffer class copies the characters starting at the index:srcBegin to index:srcEnd-1 from actual sequence into an array of char passed as parameter to function. The characters are copied into the array of char dst[] star 3 min read StringBuffer Class in Java The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c 11 min read StringBuffer Class in Java The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c 11 min read Java String getBytes() Method In Java, the getBytes() method of the String class converts a string into an array of bytes. This method is useful for encoding the strings into binary format, which can then be used for file I/O, network transmission, or encryption. It can be used with the default character set or with a specified 2 min read StringBuffer insert() in Java The StringBuffer.insert() method in Java allows us to insert a string representation of a given data type at a specified position in a StringBuffer. This method is useful when we need to modify a string at specific positions without creating a new string each time by making it more efficient than co 4 min read Like