#primitive wrappers
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Primitive wrappers in Java (e.g. Integer, Double) are indeed classes and can be mutated/modified. However, they have some limitations compared to regular classes, such as being immutable. This means that when you modify a primitive wrapper object, a new object is created instead of modifying the existing one.
In the case of HashMap<String,Integer>, if you get an Integer value from the map and modify it, a new Integer object will be created. This new object will not affect the value stored in the map.
Useful links:
That nothing to do with the immutability of wrappers. Assignment is changing a reference, not the object.
These are effectively the same concept
int a = 5;
Integer b = 10; // this is compiled as `b = Integer.valueOf(10)`
a = 50;
b = 100;
When assigning to a the 'int' value 5 is replaced by the value 10.
When assigning to b the 'Integer reference' value, initially referring to an Integer object containing 10, is replaced by a reference to an Integer object containing 100.
This is true for all usages of the = (assignment) operator.