Why don't wrapper classes for primitives have a setter?

What is the reason why Wrapper classes (such as Integer, Double, etc.) do not have a setter for their internal primitive value?

I ask about this because such functionality will simplify the calculus and make the Java language a little more flexible.

Let me give you some examples.

1) Take the following example:

Integer x = new Integer(5); x++; 

The previous code behind the scenes performs autoboxing. Sort of:

 int x_tmp = x.intValue(); x_tmp++; x = new Integer(x_tmp); // Yes that a new memory allocation 

Because of this problem, calculus execution on Wrapper is slower than execution on simple primitive types. With a setter, it would be easier to increase the internal value without allocating another object on the heap.

2) Another problem that pisses me off is the inability to write a swap function in Java, as I can do in C (using pointers) or in C ++ (pointers or links).

If I write void swap(Integer x, Integer y) , I can not get the internal value, because, and it will be impossible for me to change the values.

PS: A friend of mine suggested I consider the big picture and think in terms of concurrency and indicate immutability.

Do you have an explanation? Thanks!

+4
source share
4 answers

1) With the installer, wrapper types will be mutable. Immutability is a good thing in many ways ... slicing, general code comprehensibility, etc. Personally, it seems to me a shame that Calendar and Date are mutable, for example.

In fact, the x++; extension x++; not entirely correct - it uses Integer.valueOf , which does not always create a new value. For instance:

 Integer x = 5; x++; Integer y = 5; y++; // This prints true System.out.println(x == y); // Compare references 

Only a limited range of Integer values ​​is cached this way (the specification defines which values ​​should behave this way, but allows a wider range if the JRE wants to do this) ... but this means that it will not always create a new object.

2) Yes, Java does not have access to the link. Honestly, I very rarely think that this is a problem. How often do you really need to change the values ​​of variables?

+4
source

Wrapper classes are not commonly used unless you need to put them in a collection. If they were mutable, this would create problems when used within sets and as keys for hash tables.

Sets hashtables so that the hash value is always the same.

+6
source

Cache chains ranging from -128 to 127 require immutable integers. Consider the following code:

 Integer id = Integer.valueOf(1); // a new Integer, cached in Integer class // and somewhere else Integer key = Integer.valueOf(1); // returns the cached value 

Now, if Integer was changed and had a setter, and someone did

 key.setValue(2); // not legal Java code, just for demonstration 

this would also change the id value and so many people would surprise:

 Integer one = Integer.valueOf(1); if (one != 1) System.out.println("Surprise! I know, you expected `1`, but ..."); 
+4
source

In Java, the Strings and wrapper classes are designed as immutable to avoid accidental data changes. You can check the article below for more information.

Why are the Strings and Wrapper classes immutable in java?

+1
source

Source: https://habr.com/ru/post/1316405/


All Articles