Java — Final VS Immutability | Code Factory

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?

  1. final variable
  2. final object X
  3. Immutable variable X
  4. Immutable object

--

--