What if changing a word could break your whole program without you knowing?
Why String immutability in Java? - Purpose & Use Cases
Imagine you have a list of names written on sticky notes. If you want to change a name, you have to erase and rewrite it on the same note. But what if someone else is reading that note at the same time? They might see the name half-erased and get confused.
Changing strings directly can cause confusion and mistakes, especially when many parts of a program use the same string. If one part changes it, others might see unexpected results. This makes programs buggy and hard to fix.
String immutability means once a string is created, it cannot be changed. Instead, if you want a different string, you create a new one. This keeps all parts of the program safe and consistent, like giving each reader their own copy of the note.
String name = "Alice"; name = name.replace('A', 'E'); // reassigns name to a new string
String name = "Alice"; String newName = name.replace('A', 'E'); // creates new string
This makes programs safer and easier to understand because strings never change unexpectedly.
When multiple users access the same text in a chat app, string immutability ensures each user sees the correct message without accidental changes.
Strings cannot be changed once created.
Changing a string creates a new one instead.
This prevents bugs and keeps data safe.
Practice
String in Java is immutable?Solution
Step 1: Understand the meaning of immutability
Immutability means the object cannot be changed after creation.Step 2: Apply this to Java Strings
Java strings cannot be modified once created; any change creates a new string.Final Answer:
Once created, the string cannot be changed. -> Option AQuick Check:
String immutability = cannot change after creation [OK]
- Thinking strings can be changed directly
- Confusing immutability with constant variables
- Assuming strings change when printed
str?Solution
Step 1: Check syntax for changing characters in strings
Strings are immutable, so direct assignment likestr.charAt(0) = 'A'is invalid.Step 2: Use substring to create a new string
Concatenate "A" with the substring from index 1 to form a new string.Final Answer:
str = "A" + str.substring(1); -> Option CQuick Check:
Use substring + concat to change string [OK]
- Trying to assign directly to string characters
- Using array-like indexing on strings
- Using non-existent methods like replaceChar
String s = "hello"; s.toUpperCase(); System.out.println(s);
Solution
Step 1: Understand that strings are immutable
The methodtoUpperCase()returns a new string but does not change the original strings.Step 2: Check what is printed
Sinceswas not reassigned, printingsoutputs the original string "hello".Final Answer:
hello -> Option BQuick Check:
Original string unchanged = hello [OK]
- Assuming toUpperCase changes original string
- Expecting uppercase output without reassignment
- Confusing method effect with print output
String s = "test"; s.charAt(0) = 'T'; System.out.println(s);
Solution
Step 1: Check usage of charAt()
The methodcharAt()returns a character but cannot be assigned a new value.Step 2: Understand string immutability
Strings cannot be changed by assigning to characters; this causes a compile error.Final Answer:
Cannot assign to charAt() result; strings are immutable. -> Option AQuick Check:
charAt() result is read-only [OK]
- Trying to assign to charAt()
- Thinking strings are mutable
- Ignoring compile errors on assignment
input. Which code correctly does this considering string immutability?Solution
Step 1: Understand replace() returns new string
Thereplace()method returns a new string with replacements but does not change the original.Step 2: Assign the new string back to input
To updateinput, assign the result ofreplace()back to it before printing.Final Answer:
input = input.replace('a', 'b'); System.out.println(input); -> Option DQuick Check:
Assign replace result to variable to update string [OK]
- Calling replace() without assignment
- Trying to assign to charAt() or array elements
- Expecting original string to change automatically
Ready to prove it?
Pass the test and this pattern moves unknown
