CharAt method in AP Computer Science A

charAt(int index) is a Java String method that returns the single character (a char primitive) at the given index, where valid indices run from 0 to length() - 1. On AP CSA, you'll more often see substring(index, index + 1), since charAt is not on the exam's Java Quick Reference.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is the charAt method?

The charAt method is called on a String object and hands back the character sitting at a specific index. Strings in Java are indexed starting at 0, so "hello".charAt(0) returns 'h' and "hello".charAt(4) returns 'o'. Ask for an index outside the range 0 to length() - 1 and Java throws a StringIndexOutOfBoundsException, the same rule EK 1.15.B.1 gives you for every String index operation.

Here's the AP-specific catch. charAt returns a char, a primitive type, while the methods on the AP Java Quick Reference (length, substring, indexOf, equals, compareTo) work with Strings and ints. That's why College Board code almost always grabs a single character with str.substring(i, i + 1), which returns a one-character String instead of a char. You should recognize charAt if it shows up, but when you write FRQ code, substring is the safe, Quick-Reference-approved move. Also remember EK 1.15.A.3: Strings are immutable, so charAt only reads a character. It can never change one.

Why the charAt method matters in AP® Computer Science A

charAt lives in Topic 1.15 (String Methods) in Unit 1: Using Objects and Methods, supporting learning objective AP Comp Sci A 1.15.B, which asks you to call methods on String objects and determine the result. Understanding charAt locks in the two ideas that 1.15.B is really testing. First, zero-based indexing, where the last valid index is length() - 1, not length(). Second, the exact boundary where StringIndexOutOfBoundsException fires. Those two facts show up constantly in string-traversal code, which then carries forward into loops (Unit 2) and ArrayList/array algorithms (Unit 4). Even though charAt itself isn't on the Quick Reference, the indexing model behind it is everywhere on the exam.

How the charAt method connects across the course

substring method (Unit 1)

substring(i, i + 1) is the AP-sanctioned way to do what charAt does. The difference is the return type. charAt gives you a char like 'a', while substring gives you a one-character String like "a". Since the Quick Reference includes substring and not charAt, released FRQ solutions pull single characters with substring.

indexOf (Unit 1)

indexOf is the reverse operation. charAt takes an index and gives you the character there, while indexOf takes a String and tells you the index where it first appears (or -1 if it's not found). Pairing them mentally helps you read string-search code quickly.

string literal (Unit 1)

Every charAt call happens on a String object, and EK 1.15.A.1 says those objects often come from string literals like "hello". Because Strings are immutable (EK 1.15.A.3), charAt can inspect the characters of a literal but never modify them.

String traversal with loops (Unit 2)

The classic pattern is a for loop running i from 0 to str.length() - 1, grabbing one character per iteration. Whether the code uses charAt or substring(i, i + 1), the off-by-one logic is identical, and that loop boundary is exactly where exam questions try to trip you with a StringIndexOutOfBoundsException.

Is the charAt method on the AP® Computer Science A exam?

charAt is not one of the String methods listed on the AP Java Quick Reference, so FRQs won't require it and official solutions use substring(i, i + 1) to get single characters. That said, you can use charAt correctly in your own FRQ code without penalty, and multiple-choice questions absolutely test the underlying skill it represents. Expect MCQ stems that ask you to trace string code and predict output, spot the index where a StringIndexOutOfBoundsException occurs, or determine what a loop builds character by character. The thing you must DO is apply zero-based indexing precisely. Know that valid indices run 0 through length() - 1, and know that charAt returns a char while substring returns a String, since mixing those types up breaks comparisons (you compare chars with ==, but Strings with equals).

The charAt method vs substring(i, i + 1)

Both pull out the single character at index i, but they return different types. charAt(i) returns a char primitive, so you compare results with == (like s.charAt(0) == 'h'). substring(i, i + 1) returns a String object, so you compare with equals (like s.substring(0, 1).equals("h")). On AP CSA, substring is the one on the Java Quick Reference, which is why exam code prefers it. Using == on the substring version or equals on a char is a classic trap answer in MCQs.

Key things to remember about the charAt method

  • charAt(index) returns the char primitive at a given position in a String, with valid indices running from 0 to length() - 1.

  • Calling charAt with an index outside that range throws a StringIndexOutOfBoundsException, per EK 1.15.B.1.

  • charAt is not on the AP Java Quick Reference, so exam code typically uses substring(index, index + 1) instead, which returns a one-character String rather than a char.

  • Compare chars with == but compare Strings with equals, so the return type of charAt versus substring actually changes how you write the comparison.

  • charAt only reads characters and never changes them, because String objects are immutable (EK 1.15.A.3).

Frequently asked questions about the charAt method

What does the charAt method do in Java?

charAt(int index) is called on a String and returns the character at that index as a char primitive. For example, "java".charAt(2) returns 'v'. Indexing starts at 0 and the last valid index is length() - 1.

Is charAt on the AP Computer Science A exam?

Not on the Java Quick Reference, no. The CED's required String methods are length, substring, indexOf, equals, and compareTo, so FRQs use substring(i, i + 1) to grab single characters. You can still use charAt in your own FRQ code, and MCQs may test the same indexing logic.

What's the difference between charAt and substring?

charAt(i) returns a char primitive, while substring(i, i + 1) returns a one-character String. That changes how you compare the result: use == for the char and equals for the String. Functionally they extract the same character; only the type differs.

Does charAt change the original String?

No. Strings in Java are immutable (EK 1.15.A.3), so no String method, including charAt, can modify the String it's called on. charAt just reads and returns one character.

What happens if I call charAt with an index equal to the String's length?

You get a StringIndexOutOfBoundsException, because the last valid index is length() - 1, not length(). So "cat".charAt(3) crashes even though the String has 3 characters. This off-by-one boundary is a favorite MCQ trap.

charAt Method — AP Comp Sci A Definition & Exam Guide | Fiveable