Java — Final VS Immutability | Code Factory
1 min readMay 16, 2020
Index Page : Link
Donate : Link
- If you don’t want to perform reassignment for the variable then it is a final.
- If you don’t want to perform any changes in the object then it is a immutable.
final StringBuffer sb = new StringBuffer("Code");
sb.append("Factory");
System.out.println(sb); // CodeFactory
sb = new StringBuffer("Hello"); // CE : cannot assign a value to final variable sb
Which of the following are meaningful?
- final variable ✓
- final object X
- Immutable variable X
- Immutable object ✓