Java Interview Question

Use this advanced question and answer to prepare for your next Java interview.

What are Immutable Classes in Java? What are Immutable Classes useful for?


Immutable classes are those which cannot be changed once instantiated. The value of the object of an immutable class will always remain the same. Either the value cannot be changed or any change to the vale will result in a new immutable instance. String, Boolean and other primitive wrapper classes are all Immutable. All the variables and the class itself are declared final to prevent modification from anywhere.

Immutable classes are very useful for concurrency and multi-threaded programs. They allow using objects at the same time across any number of threads without having a conflict and without requiring any synchronization. They can also be stored in memory more efficiently when used in a multithreaded context. They can be cached easily and used in a static context.

Looking for developers?

Want to learn more about Gigster? Enter your email and we'll be in touch.