Mutable vs Immutable Objects

In short: A mutable object can be changed after it's created; an immutable object can't, so "changing" it creates a new object instead. In Python, lists, dictionaries, and sets are mutable while numbers, strings, and tuples are immutable; in Java, strings are immutable but most other objects are mutable.

A mutable object can be changed after it's created, and an immutable object can't. When you "change" an immutable object, you're really creating a new object with the new value, and the original stays exactly the same.

In Java, objects are mutable by default (strings, and wrapper classes like Integer, are notable exceptions):

public class IntegerPair { int x; int y; IntegerPair(int x, int y) { this.x = x; this.y = y; } } IntegerPair p = new IntegerPair(5, 10); // p.x = 5, p.y = 10 p.x = 50; // p.x = 50, p.y = 10

There's no way to make existing objects immutable. Even if the variable holding an object is declared final, the object's fields can still be changed:

public class IntegerPair { int x; int y; IntegerPair(int x, int y) { this.x = x; this.y = y; } } final IntegerPair p = new IntegerPair(5, 10); // p.x = 5, p.y = 10 p.x = 50; // p.x = 50, p.y = 10

That said, if you're defining your own class, you can make its objects immutable by making all fields final and private.

public class IntegerPair { private final int x; private final int y; IntegerPair(int x, int y) { this.x = x; this.y = y; } } IntegerPair p = new IntegerPair(5, 10); p.x = 50; // Compilation error: cannot assign a value to a final variable

Strings can be mutable or immutable depending on the language.

Strings are immutable in Java.

Any time you change a string (e.g.: tacking on an extra character, making it lowercase, swapping two characters), you're actually creating a new and separate copy:

String first = "first"; System.out.println(first.hashCode()); // prints something first = first + "!"; System.out.println(first.hashCode()); // different string, different hash code

But in some other languages, like C++, strings can be mutable, and we can modify them directly:

string testString("mutable?"); testString[7] = '!'; // testString is now "mutable!"

If you want mutable strings in Java, you can use a StringBuilder object:

StringBuilder mutableString = new StringBuilder("mutable?"); mutableString.setCharAt(7, '!'); // still the same object! // mutableString is now "mutable!" // convert to an immutable string String immutableString = mutableString.toString();

Or, you can convert the string to an array of characters, which will be mutable.

Mutable objects are nice because you can make changes in-place, without allocating a new object. But be careful—whenever you make an in-place change to an object, all references to that object will now reflect the change.

Mutable vs. Immutable

Mutable Immutable
Can it change after it's created? Yes, in place No—"changing" it makes a new object
Shared references A change through one reference shows up through all of them Safe to share, since nobody can change it
Cost of an update Cheap: just change it Has to copy, so building one up piece by piece (like repeatedly adding to a string) can take O(n²) time overall
Using it across threads Needs locks or other synchronization if any thread might change it Safe to share between threads without locks
As a hash map key Risky: if the key changes after you insert it, you might not be able to find it again Works great
Examples in Java Arrays, ArrayLists, HashMaps, StringBuilders, and most objects you define yourself Strings, wrapper classes like Integer, and classes like LocalDate

Neither one is better across the board. Use mutable objects when you need to make lots of updates efficiently, and immutable ones when you want values that can be shared freely without anyone changing them out from under you.

Frequently Asked Questions

What's the difference between mutable and immutable objects?

Mutable objects can be modified in place after creation; immutable objects cannot—any 'change' creates a new object instead. Immutable objects are safe to share and to use as hash keys, while mutable objects are cheaper to update.

Are strings mutable?

It depends on the language. Strings are immutable in Java, Python, JavaScript, and C#, so every 'change' builds a new string. They're mutable in C++ (std::string) and in Ruby (unless they're frozen).

Which Python types are immutable?

Numbers (int, float), strings, tuples, frozensets, and booleans are immutable. Lists, dictionaries, and sets are mutable.

Why does mutability matter in interviews?

It explains gotchas like shared references, mutable default arguments, and why only immutable (hashable) objects can be used as dictionary keys or set elements.

Last updated: October 21, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .