1. What is String?

String in Java is a class used to represent a sequence of characters.

String name = "Rahul";

Here "Rahul" is a String object.

Java provides many built-in methods to work with strings, such as length(), charAt(), substring(), equals(), etc.


2. Why String is Immutable?

Immutable means once a String object is created, its value cannot be changed.

String s = "Hello";

s = s + " World";

The original "Hello" String is not modified. Java creates a new String object containing "Hello World", and s is made to refer to the new object.

Immutability provides benefits such as security, thread-safety, String Pool optimization, and predictable behavior.


3. String Immutability

Once a String object exists:

String s = "Hello";

you cannot modify that "Hello" object.

For example:

s.concat(" World");

doesn't change the original String.

If you want the result: