I have a HashMap:
private HashMap<String, Integer> cardNumberAndCode_ = new HashMap<String, Integer>();
And later I do this:
Integer balance = cardNumberBalance_.get(cardNumber);
System.out.println(balance);
balance = 10;
Integer newBalance = cardNumberBalance_.get(cardNumber);
System.out.println(newBalance);
First it prints 1000
, and the second time it's printing 1000
, the value does not change. Why is Java returning the Integer by value and not by reference?
Java is officially always pass-by-value. The question is, then, “what is passed by value?” As we have said in class, the actual “value” of any variable on the stack is the actual value for primitive types (int, float, double, etc) or the reference for reference types.
Object references are passed by value The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.
Java pass the reference by value. Putting it very concisely, this confusion arises because in Java all non-primitive data types are handled/accessed by references. However, passing is always be value. So for all non-primitive types reference is passed by its value.
1) Yes, it returns a reference to the object. 2) If the method is private, then it can only be called from within the class itself. 5) Object.
The get
method returns a copy of the reference to the stored integer...
Assigning a new value to the variable storing this copy in order to point to the value 10
will not change the reference in the map.
It would work if you could do balance.setValue(10)
, but since Integer
is an immutable class, this is not an option.
If you want the changes to take affect in the map, you'll have to wrap the balance in a (mutable) class:
class Balance {
int balance;
...
}
Balance balance = cardNumberBalance_.get(cardNumber);
System.out.println(balance.getBalance());
balance.setBalance(10);
Balance newBalance = cardNumberBalance_.get(cardNumber);
System.out.println(newBalance.getBalance());
But you would probably want to do something like this instead:
cardNumberBalance_.put(cardNumber, 10);
The Integer variable contains a reference to an Object. The Integer object is immutable and you cannot change it. When you perform
balance = 10; // replace the previous Integer reference with a different one.
The normal way to do this is to use
cardNumberBalance_.put(cardNumber, 10);
An alternative which is not used so often is to use AtomicInteger or use your own MutableInteger
private final Map<String, AtomicInteger> cardNumberAndCode_ = new HashMap<String, AtomicInteger>();
AtomicInteger balance = cardNumberBalance_.get(cardNumber);
balance.set(10);
Java does not support pass-by-reference (and return-by-reference). See Is Java "pass-by-reference" or "pass-by-value"?
The result of assignment
balance = 10;
is that a new instance of Integer
is created with value of 10, and its reference is assigned to balance
variable. It does not change the object that you get from the map, that is the object stored in the map is unchanged.
If you need to change the value of balance, you have to wrap it in a mutable class just like aioobe describes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With