-1

i know the reason, why String is immutable but my question is why the concept of immutability sticks to only String class in java , why it won't apply to others.

one of the reason i found why it is immutable is HERE

but why not all other classes like Integer and so on.. how java people decided changing their values might not effect ..

please explain .

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
saikiran
  • 2,247
  • 5
  • 27
  • 42
  • Java (being an "imperative" language) utilizes side-effects quite a bit, and such is found all over in the standard library. However, there is nothing that *mandates* that an object is mutable - any object, like String, that *doesn't* allow a way to mutating the state of the object is well, an immutable object. Other immutable types include the primitive wrappers (*like Integer*) and Pattern. – user2864740 Mar 03 '14 at 08:12
  • (Immutable objects are often *easier* to reason about and reduce "surprises", which is why I recommend striving for immutability where practical - more than one person has been surprised by mutable strings in Ruby! However, a "string pool" is *not* the cause of immutability as immutability exists without "string pools", but rather an application/utilization of such immutability!) – user2864740 Mar 03 '14 at 08:14
  • 3
    `but why not all other classes like Integer and so on` - [but they are ...](http://stackoverflow.com/questions/5560176/is-integer-immutable) – Andreas Fester Mar 03 '14 at 08:20
  • 1
    All the primitive wrapper classes are immutable. And several other ones as well. – JB Nizet Mar 03 '14 at 08:25
  • 3
    What example? Look at the primitive wrapper classes javadoc (Integer, Long, Double, Boolean, etc.) They're all final, and they all don't contain any method allowing to change the value they wrap. Once you have constructed an Integer, it will always have the same int value. There's no way to change it. That's what immutability means. – JB Nizet Mar 03 '14 at 08:31

3 Answers3

1

The point is that you could confuse a String (which is an Object) with a primitive type. In fact, The way strings typically appear in Java programs can lead to confusion. There are shortcuts available for strings that are not available for other objects in Java. These shortcuts are highly used and they give strings the appearance of a primitive data type. But, strings are objects and they possess all the attributes and behaviours of objects in Java

Billyjoker
  • 729
  • 1
  • 10
  • 31
1

When designing a class, to make it immutable or not is a design choice and usually a tradeoff. The classes String and Integer were made immutable for many reasons including security.

A class like java.util.Random is all about managing some state, so it has to be mutable.

A class like java.awt.Rectangle is mutable, which has the advantage of saving memory management costs when you modify one rather than create a new one, but unfortunately it means that when a Rectangle instance that's shared (e.g. by two different window objects), changing it for one of them will change it out from under the others, causing unhappy surprises. Modern garbage collectors are very efficient [I hear they're more efficient than C's malloc() and free()] so mutability may not actually save memory management overhead, certainly not if you often have to copy objects to protect against accidental shared changes.

You can freely share an immutable object without worrying about it changing out from under you.

Jerry101
  • 12,157
  • 5
  • 44
  • 63
0

Integer is immutable, so each operation on it creates a new instance.

Immutable does not mean that a refernece can never assign another value. For example, String is immutable too, but we can do this:

String ex = "good";    // ex == "good"
ex = ex + "morning";    // now ex == "goodmorning"

So what happened there? Since String is immutable, clearly "ex" was not changed. But it now being assigned something different. This is because "ex" is now a completely newly instantiated object.

So this is same the case of Integer.

RPaul
  • 946
  • 10
  • 11
  • 2
    it doesn't throw an exception. It doesn't even compile – iberbeu Mar 03 '14 at 08:32
  • what it wil give without final . by using final it became constant. the value of it won't be changed. but how it supports concept of immutability @RPaul – saikiran Mar 03 '14 at 08:32
  • `final` is a slightly separate issue. A class can have no public fields and no methods to change its state without being final. Final just means it's not allowed to have subclasses. But if your class isn't final, then you can't prevent the creation of subclasses with extra instance variables and methods to change the values of those variables. – David Knipe Mar 03 '14 at 08:47
  • @saikiran I have changed my answer,I was not explaining the concept of immutable there – RPaul Mar 03 '14 at 09:42