#primitive wrappers

1 messages ยท Page 1 of 1 (latest)

next ruinBOT
#

<@&987246399047479336> please have a look, thanks.

#

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>.

#
TJ-Bot
Do primitive wrappers act like classes or do they have rules like primitives where they cannot be mutated/modified? Just curious because if i had HashMap<String,Integer> from when I tested it, if I get the Integer and modify it, it doesnt change the hashma

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:

  1. Primitive Data Types in Java
  2. Integer vs int in Java
  3. Understanding Wrapper Classes in Java
lime vale
#

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.