Skip to main content

Immutable in AP Computer Science A

In AP Computer Science A, an immutable object is one whose attributes cannot be changed once it is created. String, Integer, and Double objects are immutable, so methods like substring or concat return a new object instead of changing the original.

This definition is part of Fiveable's AP Computer Science A review, which also has guided practice questions.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is Immutable?

An immutable object is locked in once it's built. After Java creates it, its attributes (its state) can never change. The CED names three immutable classes you need to know. String is covered in EK 1.15.A.3, and Integer and Double are covered in EK 4.7.A.1. All three live in the java.lang package.

The tricky part is that your variable can still change. When you write str += "!", Java doesn't edit the old String. It builds a brand-new String and points str at it. The old one is untouched. Think of it like a printed receipt. You can't erase what's printed, but you can print a new receipt and throw the old one in your pocket. That's why EK 1.15.A.3 says methods called on a String "do not change the content of the String object." They hand you back a new one.

Why Immutable matters in AP® Computer Science A

Immutability shows up in two CED topics across two units. In Unit 1 (Topic 1.15 String Methods) it supports learning objectives 1.15.A and 1.15.B. You create, combine, and call methods on Strings, and you need to predict the result. In Unit 4 (Topic 4.7 Wrapper Classes: Integer and Double) it supports 4.7.A. You use Integer and Double objects made from primitives and determine what happens. The exam rarely asks you to define the word. Instead, it gives you code that looks like it changes a String or Integer and checks whether you know it didn't. If you misunderstand immutability, you'll miss a whole family of code-tracing questions.

How Immutable connects across the course

String methods return new Strings (Unit 1)

Every String method on the Java Quick Reference, like substring, gives you a result without touching the original. So s.substring(1) alone on a line does nothing useful. You have to save the result, as in s = s.substring(1);, or it's thrown away.

Wrapper classes and autoboxing (Unit 4)

Integer and Double follow the same rule as String. When you write Integer n = 5; n++;, Java unboxes the value, adds 1, and autoboxes a new Integer. The original Integer object never changed. Only the reference moved.

Mutable objects like arrays and ArrayLists (Unit 4)

Mutable is the opposite of immutable. If a method changes an element of an array or an ArrayList passed to it, the caller sees the change. If a method does s += "ly" on a String parameter, the caller's String is unchanged. That contrast is the whole point.

The equals method vs == (Unit 1)

Since operations on Strings keep producing new objects, two Strings with the same characters may be different objects. That's why you compare String contents with equals, not ==.

Is Immutable on the AP® Computer Science A exam?

Immutability is tested almost entirely through code tracing in the MCQ section. Watch for these common setups.

  • A method call with no assignment. String str = "Hello"; str.concat(" World"); leaves str as "Hello" because the new String was never stored.
  • Concatenation order. String str3 = str1 + str2; str1 += "Programming"; then printing str3 still gives "JavaScript". Changing str1 later doesn't reach back into str3, since str3 is its own separate object.
  • Passing a String to a method. A method like modify(String s) { s += "ly"; } called on text = "quick" leaves text as "quick". The method only reassigned its local copy of the reference.
  • Vocabulary MCQs. These ask which term describes why Integer and Double attributes can't be modified after creation. The answer is immutable.

No released FRQ has used the word verbatim. You still need it on FRQs, because string-processing methods only work if you store results, as in word = word.substring(1);.

Immutable vs Final Keyword

final is about the variable. A final variable can't be reassigned to point at something else. Immutable is about the object. An immutable object's contents can't change. They're independent. A regular String s can be reassigned (s = "new") even though the String object itself is immutable. Meanwhile, a final array variable can't be reassigned, but you can still change its elements because arrays are mutable.

Key things to remember about Immutable

  • An immutable object's attributes cannot change after it is created, and in AP CSA the key immutable classes are String, Integer, and Double.

  • String methods like substring and concat return a new String, so you must assign the result to a variable to keep it.

  • Using += on a String or ++ on an Integer creates a new object and moves the reference to it, leaving the old object unchanged.

  • Passing a String to a method and reassigning the parameter inside that method does not change the caller's String.

  • Immutable describes the object, while final describes the variable, so a non-final String variable can still be reassigned.

  • Because String operations create new objects, compare String contents with equals rather than ==.

Frequently asked questions about Immutable

What does immutable mean in AP Computer Science A?

It means an object's attributes cannot be changed once the object is created. The CED identifies String (EK 1.15.A.3), Integer, and Double (EK 4.7.A.1) as immutable classes.

If Strings are immutable, why does str += "a" work?

It works because it doesn't change the original String. Java builds a new String with "a" added and points str at that new object. The variable changed, but no String object did.

Does calling str.concat(" World") change str?

No. concat returns a new String, so unless you write str = str.concat(" World");, str still holds its original value, like "Hello".

What's the difference between immutable and final in Java?

final stops a variable from being reassigned, while immutable means the object itself can't be modified. A final array variable can't point to a new array, but its elements can still change because arrays are mutable.

Are Integer and Double immutable on the AP CSA exam?

Yes. EK 4.7.A.1 states that once an Integer or Double object is created, its attributes cannot be changed. Operations like n++ on an Integer unbox the value, compute, and autobox a brand-new Integer.