1.15

String Manipulation

Strings are immutable objects created from literals or constructors, combined via concatenation that always produces new String objects.

1525% of exam
Understand It
Ace It
Context

What this topic is and why it exists

Imagine you're writing a message in permanent marker on a whiteboard — once the ink dries, you can't change what's there.
You can only grab a fresh whiteboard and write something new.
That's exactly how Strings work in Java.
When you create a String, whether by simply writing `"hello"` or using `new String("hello")`, that sequence of characters is locked in place forever.
It's *immutable*.
Every method you call on a String — every trim, every uppercase conversion — doesn't touch the original.
It hands you back a brand-new String instead.
Now here's where it gets fun.
When you use `+` or `+=` to glue Strings together, Java creates an entirely new String object from the pieces.
And Java is surprisingly generous about what you can concatenate.
Toss a number like `42` next to a String?
Java quietly converts it into `"42"` and stitches them together.
Concatenate any object at all?
Java calls that object's `toString()` method behind the scenes to get a String representation.
So `"Score: " + 42` becomes `"Score: 42"`, and `"Player: " + myPlayer` becomes whatever `myPlayer.toString()` returns.
The key insight: you're never modifying Strings — you're always building new ones.
1 / 9