Skip to main content
← AP CSA Study Guide

Unit 4 · Moderate

Strings

Text as an immutable object. Two facts generate most of the unit: indexing starts at zero and stops one before the length, and no String method ever changes the String it was called on.

What a strong answer looks like

A strong Unit 4 answer marks the start and end index before evaluating, and states explicitly that the original String is unchanged.

Topics in this unit

1

String Indexing

Practice this topic →

Know

Indexes run from zero to length minus one. The length itself is never a valid index.

Apply

Write the index beneath each character before evaluating any call.

Watch out

Using length as an index, which is the standard out-of-bounds error.

Study move

Label every index of a short word, then predict the result of two charAt calls.

2

substring and indexOf

Practice this topic →

Know

substring includes its start index and excludes its end index. indexOf returns the first match, or negative one when there is none.

Apply

Mark both boundaries on paper before evaluating, and handle the not-found case explicitly.

Watch out

Treating the end index as inclusive, which produces a string one character too long.

Study move

Extract the portion of a string before a delimiter, then test it when the delimiter is absent.

3

String Immutability

Practice this topic →

Know

String methods return new Strings; they never modify the original. A result that is not assigned is discarded.

Apply

Assign the result of every String operation, or it has no effect.

Watch out

Calling a method and expecting the variable to change, which silently does nothing.

Study move

Write two lines that differ only by whether the result is assigned, and state both outputs.

4

String Traversal

Practice this topic →

Know

Traversing a String means looping over its indexes, most often to compare adjacent characters or build a new String.

Apply

Choose bounds that match the comparison: adjacent-pair loops must stop one index early.

Watch out

Running an adjacent-pair loop to the final index, which reads past the end.

Study move

Write a loop that counts adjacent equal characters and check it on a one-character string.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Writing indexes down rather than counting them mentally
  • Treating every String method as returning a value rather than changing one
  • Adjusting loop bounds when comparing pairs

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • StringBuilder. Useful in class projects; not required by the exam.
  • Regular expressions. Occasionally shown, but outside the tested subset.

Mastery checklist

  • Give the valid index range for a String of length n.
  • Evaluate a substring call with both boundaries marked.
  • Explain what indexOf returns when there is no match.
  • Explain why an unassigned String operation has no effect.

Check yourself

  • Why is length itself never a valid index?
  • Which of substring start and end is inclusive?
  • What happens to the original String when a method is called on it?

Modeling drill

Write a method that returns the portion of an email-like string before the at sign, and state its behavior when there is no at sign.

IndexsubstringindexOfImmutabilityTraversalOut of bounds