Immutable class in Java is a class that cannot be broken or whose content cannot be changed once an object is created. Immutable classes in java are like double, float, long, integer, short, byte, boolean, string, etc. To create an immutable class, we must create a final class as the content of the final class cannot be changed.

Immutable Class In Java

Immutable objects are the instances whose state doesn’t modify after it has been initialized. For instance, the  String is an immutable class, and once instantiated its value never changes.

The immutable class is good for the caching purpose because you don’t need to worry about the value changes.

Another benefit of the immutable class is that it is inherently thread-safe, so you don’t need to worry about the thread safety in the  multi-threaded environment.

#java #thread-safe

How to Create Immutable Class in Java Example
1.30 GEEK