What is immutable (Java)? In Java, immutability means an object cannot be changed after it is created. A good example is String. When you modify a string, you are not changing the original object, you are creating a new one. Why this matters: If you call a method like concat() and don’t store the result, nothing changes. So even though it looks like you’re modifying the value, Java keeps the original object unchanged. #java #interview #immutable #certification
The String example does not really capture the essence of immutability in Java. It shows the effect, but not the underlying principles. Because of interning and JVM optimizations, String can behave in ways that hide what immutability actually means. As a result, it is easy to misunderstand why it is immutable in the first place. A clearer way to explain immutability is through a custom class where fields are final, state cannot be changed after construction, and any mutable data is protected with defensive copies. That makes the concept explicit and shows what actually guarantees immutability in Java.
The important point here is not about immutability BUT the fact that the method returns a value.
You guys are so funny with such posts everyday, really