Is Wrapper Class Immutable in Java?

Is Wrapper Class Immutable in Java?

All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.

Why are wrapper classes immutable in Java?

The wrapper classes are immutable because it just makes no sense to be mutable. Consider following code: int n = 5; n = 6; Integer N = new Integer(n); At first, it looks straightforward if you can change the value of N, just like you can change the value of n.

Is wrapper class thread safe?

Immutable objects are automatically thread-safe, the overhead caused due to use of synchronisation is avoided. ... Once created the state of the wrapper class immutable object can not be changed so there is no possibility of them getting into an inconsistent state.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.