Java StringBuffer charAt() Method Last Updated : 09 Dec, 2024 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 Java StringBuffer charAt() Method J juhisrivastav Follow Improve Article Tags : Java Java-Strings java-StringBuffer Practice Tags : JavaJava-Strings Similar Reads Java StringBuffer capacity() Method In Java, the capacity() method is used in the StringBuilder and StringBuffer classes to retrieve the current capacity of the object. The capacity is the amount of space that has been allocated to store the string that can grow dynamically as needed.Example 1: The below Java program demonstrates the 3 min read 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 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 Like