Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java return by reference or by value

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?

like image 747
Merni Avatar asked Sep 04 '11 07:09

Merni


People also ask

Is Java pass by value or reference prove it?

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.

Do objects get passed by reference or value in Java?

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.

Does Java pass by reference?

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.

Are objects returned by reference in Java?

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.


4 Answers

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);
like image 177
aioobe Avatar answered Oct 22 '22 23:10

aioobe


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);
like image 39
Peter Lawrey Avatar answered Oct 23 '22 00:10

Peter Lawrey


Java does not support pass-by-reference (and return-by-reference). See Is Java "pass-by-reference" or "pass-by-value"?

like image 1
ykaganovich Avatar answered Oct 22 '22 23:10

ykaganovich


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.

like image 1
Alexey Ivanov Avatar answered Oct 23 '22 01:10

Alexey Ivanov