0

I read in this question that Java is always pass-by-value. And so even references are passed by value.

I don't understand what this means, can somebody clarify this for me please?

Community
  • 1
  • 1
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • 1
    Try to follow the accepted answer in that question **line by line** until you get it, it will become clear after some attempts. Also read the definition of *reference*, it must be clear in order to understand. – Christian Tapia Mar 09 '14 at 03:54
  • A primitive type variable holds a raw value. An object variable holds a "memory address" that points to the object. So when you pass either of these around, it's those values which are transferred. – asteri Mar 09 '14 at 03:55

4 Answers4

4

Given this

Object ref = new Object();

ref is actually storing a value, some address to an object. Let's say 1234.

When you pass ref around

public void method(Object passed) {...}
...
method(ref);

Java actually copies the value of the reference and assigns it to the parameter. So, passed will also have the value 1234.

Similarly, if you had

Object otherRef = ref;

the value 1234 would be copied and assigned to otherRef.

If you then re-assign otherRef, like

otherRef = new Object();

that would assign a new value to otherRef, but ref would still have the same value as before. That's what pass by value is.

When you call a method

ref.toString();

Java uses the value of the reference to find the referenced object and invoke the method. This is called dereferencing.


You might want to go through the JPDA javadoc, starting with StackFrame. Go through the fields and types and you'll start to understand how everything is mapped. For example, it has a getValues(..) method which returns a Map<LocalVariable, Value>. That should tell you that a variable doesn't actually store anything. Instead, it is mapped to a value, where that value may be all sorts of things.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Probably unrelated but your *all sorts of things* link interested me: is **null** neither a primitive nor a reference type? – Ogen Mar 09 '14 at 04:22
  • @Clay Well, this isn't exactly representative. In JPDA, there is no `Value` for `null`. `null` is the value. That might be more confusing. Think of `null` as a reference value to a special location meaning nothing, say address `0`. – Sotirios Delimanolis Mar 09 '14 at 04:24
  • @Clay `null` is explained in the JLS, [here](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.7). It has the `null` type. – Sotirios Delimanolis Mar 09 '14 at 04:25
1
int a = 5;

public void foo(int num) {
num = num + 5;
System.out.println(num);
}

foo(a);

System.out.println(a);

In the above code, a is passed into foo() by value. That means that a new variable is created with the scope of foo() that has the same initial value as a. When the value of num is changed, the value of a is not changed. The first println() will print a 10, the second will print a 5. In c++ you could pass a in by reference, which means that the value of a would be changed too.

michaelAdam
  • 1,119
  • 14
  • 30
0

I can try, in some other languages you have the option of passing something by a pointer (e.g. a reference to a memory region). Java calls every function with a value (not a pointer), but the value of an Object is a reference to a memory region. See also the default toString() in your own classes.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I always find this a good example:

Dog foo = new Dog("Rocky");
modifyDog(foo);
System.out.println(foo.name); //Alice

public void modifyDog(Dog aDog)
{
   aDog.name = "Alice"; //here you change the field 'name' of the object located in memory currently referenced by foo and aDog variables. 
   aDog =  new Dog();   //since aDog is a copy of foo then this won't recreate foo object
}
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44