Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Integers immutable in Java?

I know Integers are immutable in Java. But why it is designed this way?

I went through other answers before asking this question:

Is Integer Immutable

i++ still working for immutable Integer in Java?

Why are Java wrapper classes immutable?

But I couldn't find the use case which mandates the Integer to be immutable. Are there any technical reasons like there are for String?

  1. String is used as parameter in network connection, database URLs etc. It could easily be compromised if it was mutable.
  2. To support StringPool facility.
  3. To support class loading mechanism in which Strings are used as arguments. String being mutable results in a wrong class being loaded.

I understand there are wrappers like AtomicInteger for mutable.

UPDATE:

From the conversation, there is no universal reason that could mandate the Integers being immutable. However by doing immutable it provides some bonus as mentioned in the answers.

Such as this quote from Andrey

possibility to cache.

Others are reducing global state

easier multithreading

like image 232
Mani Avatar asked Apr 01 '14 18:04

Mani


People also ask

Why is integer class immutable in Java?

You can't alter an immutable object! A: The answer to your question is simple: once an Integer instance is created, you cannot change its value. The Integer String , Float , Double , Byte , Long , Short , Boolean , and Character classes are all examples of an immutable class.

Are integers mutable or immutable?

int is immutable in nature, so we can't change or update the int data-type. As we read earlier, that immutable objects change their memory address when they get updated. We assigned some integer value to a variable.

Is integer in Java mutable?

For example, In Java, String, Integer, Double are Immutable classes, while StringBuilder, Stack, and Java array are Mutable.

Is int [] immutable in Java?

Integer (and other primitive wrapper classes) are immutable.


2 Answers

You won't find a mandatory reason why java.lang wrappers must be immutable. Simply because it's a design decision. They could have decided otherwise. The language designers had to choose between mutable and immutable. And they chose immutable. That's it.

There are some compelling (IMO) reasons though to make them immutable:

It's consistent with String. The same reasoning you provided for String to be immutable applies to Integer etc. as well (e.g. think of a port number in a property map). This generally applies to any mutable type.

Immutable types rule out a plethora of hard to find mistakes one can make where one involuntarily changed an objects member value by modifying the value obtained through a getter. It saves a lot of defensive copying when the type is immutable. The most infamous example is java.util.Date, which is generally a pain to use because it's mutable (API issues aside).

Also immutable types allow for the use of shared instances, like e.g. Integer does for commonly used values (see Integer.valueOf(int)).

like image 69
Durandal Avatar answered Sep 30 '22 19:09

Durandal


Can the identity of the value 1 ever change? Can it become 2? No. That's why Integer and other numeric types are immutable. They're meant to model that identity.

like image 34
Sotirios Delimanolis Avatar answered Sep 30 '22 20:09

Sotirios Delimanolis