Both String and StringBuilder are backed by arrays and charAt() gets the value at the specified index.

Since String is immutable, it must make a defensive copy before returning the array. So it has some additional overhead. But imo, this would only be a problem for very large strings or repeated conversion of many strings to arrays. It all depends how how you are using it.

Answer from WJS on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-charat-in-java-with-examples
StringBuilder charAt() in Java with Examples - GeeksforGeeks
October 15, 2018 - // Java program to demonstrate // the charAt() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object StringBuilder str = new StringBuilder(); // add the String to StringBuilder Object str.append("Geek"); // get char at position 1 char ch = str.charAt(1); // print the result System.out.println("StringBuilder Object" + " contains = " + str); System.out.println("Character at Position 1" + " in StringBuilder = " + ch); } }
Discussions

How is String charAt implemented in Java?
if you go to line 111 you'll see that the inside of a String is a char[] already More on reddit.com
🌐 r/learnprogramming
8
1
August 30, 2021
Appending character at the front in the StringBuilder

just do a .reverse on the sb.toString() at the end.. surely that's 10x more readable

More on reddit.com
🌐 r/javahelp
5
3
October 14, 2016
How to set a character of a String?
Hi, I would like to know how to set a character of an String object. It's just like the opposite of charAt() method which returns the char at a given position. TIA More on thecodingforums.com
🌐 thecodingforums.com
5
October 31, 2006
Replacing a single specific character in a string. - Processing Forum
stringbuffer (or stringbuilder) seems to be a good choice. it converts the string to a char-array and then sets the new char at the specified index. you can "extract" this functionality ( as you can see in "replaceCharAt_v1" ) to make it a bit faster, because you avoid using the stringbuffer ... More on forum.processing.org
🌐 forum.processing.org
October 20, 2012
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.stringbuilder.chars
StringBuilder.Chars[Int32] Property (System.Text) | Microsoft Learn
The index parameter is the position of a character within the StringBuilder. The first character in the string is at index 0. The length of a string is the number of characters it contains.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_charat.htm
Java StringBuilder charAt() Method
public class StringBuilderCharAt { public static void main(String args[]) { StringBuilder sb = new StringBuilder("Tutorialspoint"); int id1 = 6; int id2 = 7; System.out.println("The character at " + (id1 + id2) + " index is: " + sb.charAt(id1 + id2)); } }
🌐
Educative
educative.io › answers › what-is-stringbuildercharat-in-java
What is StringBuilder.charAt in Java?
System.out.println("The character at index 1 is : " + str.charAt(0) +", char code is : " + str.codePointAt(1)); ... We created a StringBuilder and appended the 😃 emoji to it.
Find elsewhere
🌐
Benchresources
benchresources.net › home › java › java – stringbuilder charat() method
Java - StringBuilder charAt() method
September 28, 2022 - package in.bench.resources.stringbuilder.methods; public class StringBuilderCharAtMethod { public static void main(String[] args) { // StringBuilder StringBuilder sb = new StringBuilder("Google.com"); // return character value at 3th index position char charAt1 = sb.charAt(3); // print to console System.out.println("1.
🌐
W3Schools
w3schools.com › java › java_strings.asp
W3Schools.com
charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
April 21, 2026 - Specified by: length in interface ... Returns: true if length() is 0, otherwise false · Since: 1.6 · public char charAt(int index) Returns the char value at the specified index....
🌐
BeginnersBook
beginnersbook.com › 2014 › 08 › java-stringbuilder-charat-method
Java StringBuilder charAt()
Returns the character present at the index that we pass as an argument to the charAt() method. class JavaExample{ public static void main(String[] args) { StringBuilder sb = new StringBuilder("TEST STRING"); System.out.println("String is: "+sb); for(int i=0; i<sb.length(); i++){ System.out...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › buffers.html
The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The StringDemo program that was listed in the section titled "Strings" is an example of a program that would be more efficient if a StringBuilder were used instead of a String. StringDemo reversed a palindrome. Here, once again, is its listing: public class StringDemo { public static void main(String[] args) { String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); char[] tempCharArray = new char[len]; char[] charArray = new char[len]; // put original string in an // array of chars for (int i = 0; i < len; i++) { tempCharArray[i] = palindrome.charAt(i); } // reverse array of chars for (int j = 0; j < len; j++) { charArray[j] = tempCharArray[len - 1 - j]; } String reversePalindrome = new String(charArray); System.out.println(reversePalindrome); } } Running the program produces this output: doT saw I was toD ·
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-setcharat
Java StringBuilder setCharAt()
public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("X to Y"); System.out.println("Old Sequence: "+sb); //set first char as 'A' sb.setCharAt(0, 'A'); //set last char as 'Z' sb.setCharAt(sb.length()-1, 'Z'); System.out.println("New Sequence: "+sb); } }
🌐
Scaler
scaler.com › home › topics › java › stringbuilder in java with examples, methods and constructors
StringBuilder in Java with Examples, Methods, and Constructors - Scaler Topics
May 3, 2023 - Default capacity is 16 and the new capacity is (16+1)*2 i.e 34. ... Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L. ... char charAt(index): This method is used for getting character value present at the index of the StringBuilder sequence.
🌐
Dot Net Perls
dotnetperls.com › stringbuilder-java
Java - StringBuilder Examples - Dot Net Perls
September 14, 2024 - import java.lang.StringBuilder; public class Program { public static void main(String[] args) { StringBuilder builder = new StringBuilder("magic"); // Loop over the characters in this StringBuilder. for (int i = 0; i < builder.length(); i++) { System.out.println(builder.charAt(i)); } } } m a g i c ·
🌐
The Coding Forums
thecodingforums.com › archive › archive › java
How to set a character of a String? | Java | Coding Forums
October 31, 2006 - It's just like the opposite of charAt() method which returns the char at a given position. Click to expand... No can do. You can construct a new String like this: package cljp; import java.io.PrintWriter; public class Main { private static ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 462971 › charat-but-for-int-s
java - charAt() but for int's? | DaniWeb
This is based on class StringBuilder which is more flexible to manipulate Strings than String class. class FigAt { public static int figAt(long number, int pos){ return String.valueOf(number).charAt(pos) - '0'; } public static long figDel(long number, int pos){ return Long.valueOf(new StringBuilder(String.valueOf(number)).deleteCharAt(pos).toString()); } public static void main(String args[]) { long number = 123456789; System.out.println("Long number is " + number); int i = 7; System.out.println("Figure at " + i + " is " + figAt(number, i)); i = 4; System.out.println("Figure at " + i + " deleted " + figDel(number, i)); /* javac FigAt.java java FigAt Long number is 123456789 Figure at 7 is 8 Figure at 4 deleted 12346789 */ } }
🌐
Processing Forum
forum.processing.org › topic › replacing-a-single-specific-character-in-a-string
Replacing a single specific character in a string. - Processing Forum
October 20, 2012 - stringbuffer (or stringbuilder) seems to be a good choice. it converts the string to a char-array and then sets the new char at the specified index. you can "extract" this functionality ( as you can see in "replaceCharAt_v1" ) to make it a bit faster, because you avoid using the stringbuffer ...
🌐
Brainly
brainly.com › computers and technology › high school › what returns the last character in a stringbuilder variable named strbuf? select one: a. strbuf.charat(strbuf.length() - 1) b. stringbuilder.charat(strbuf.length() - 1) c. stringbuilder.charat(strbuf.capacity() - 1) d. strbuf.charat(strbuf.capacity() - 1)
What returns the last character in a StringBuilder variable ...
March 5, 2020 - Therefore, The correct answer to the question is A. strBuf.charAt(strBuf.length() - 1). Answered by QWCyan•8.6K answers•742.5K people helped ... To get the last character of a StringBuilder variable strBuf, use strBuf.charAt(strBuf.length() ...