views:

15923

answers:

26

I always thought Java was pass by reference, however I've seen a couple of blog posts (e.g. this blog) that claim it's not. I don't think I understand the distinction they're making. Could someone explain it please?

+11  A: 

For primitives (int, long etc) it is pass by value the actual value (e.g. 3)

For Objects you pass by value the reference to the object.

So if you have doSomething(foo) and public void doSomething(Foo foo) { .. } the two Foos have copied references that point to the same objects.

SCdF
+26  A: 

Java passes references by value.

So you can't change the reference that gets passed in.

ScArcher2
A: 

If you create a method which receives an int for example and change the value within the method, the caller will not see the value you've just assigned.

int value = 1;
invokeSomeMethod( value ); // value get assigned with value 9
System.out.println( value ); // value printed will be 1

However, you can still an object which encapsulate a primitive value

SomeObject obj = new SomeObject();
obj.setValue(1);
invokeSomeMethod( obj ); // obj.setValue(9) is invoked here
System.out.println( obj.getValue() ); // value printed will be 9

So Java is not by reference when dealing with first-level objects (like primitives) but for real objects, you can actually change it's internal values (using getter/setter) and the caller's object will be affected.

Shadow_x99
A: 

Basically, reassigning Object parameters doesn't affect the argument, e.g.,

private void foo(Object bar) {
    bar = null;
}

public static void main(String[] args) {
    String baz = "Hah!";
    foo(baz);
    System.out.println(baz);
}

will print out "Hah!" instead of NULL. The reason this works is because bar is a copy of the value of baz, which is just a reference to "Hah!". If it were the actual reference itself, then foo would have redefined baz to null.

Hank Gay
+3  A: 

Java passes references to objects by value.

John Channing
A: 

Pass by reference, as the blog you referenced seems to discuss it, I believe refers to languages that will not evaluate arguments to function unstil the argumants are actually used. Java eagerly evaluates the arguments to its functions (aka methods), though.

For example, suppose you have a method like this:

int add(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return a + b;
    }
}

And you want to call it like this:

add(getFirstArg(), getSecondArg());

Since both arguments in this call are themselves calls to other methods, Java will first evaluate them, so that it knows what arguments to give to add(). In other words, it determines what values to pass to add(). In pass by value, the arguments are always evaluated, even if they don't need to be. If Java used pass by reference, and getSecondArg() evaluated to 0, then getFirstArg would never be evaluated. It's reference would just be returned as the result of add().

Does that make it more clear, or did I just manage to muddy the waters some more?

pkaeding
+127  A: 

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) {
  d.name == "Max"; // true
  d = new Dog("Fifi");
  d.name == "Fifi"; // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name == "Max"; // true

In this example aDog.name will still be "Max". "d" is not overwritten in the function as the object reference is passed by value.

Likewise:

public void foo(Dog d) {
  d.name == "Max"; // true
  d.setname("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name == "Fifi"; // true
erlando
Isn't it slightly confusing the issue with internal details? There's no conceptual difference between 'passing a reference' and 'passing the value of a reference', assuming that you mean 'the value of the internal pointer to the object'.
izb
But there is a subtle difference. Look at the first example. If it was purely pass by reference, aDog.name would be "Fifi". It isn't - the reference you are getting is a value reference that if overwritten will be restored when exiting the function.
erlando
beware however that integral types are passed by value and not by reference. Also immutable types are passed by reference but "kinda" works as passing them by value: the most common example would be String. This allow for internal space optimization of immutable objects.
Lorenzo Boccaccia
Great examples...
Andrew G. Johnson
@Lorenzo: No, in Java everything is passed by value. Primitives are passed by value, and object references are passed by value. The objects themselves are never passed to a method, but the objects are always in the heap and only a reference to the object is passed to the method.
Esko Luontola
My attempt at a good way to visualize object passing: Imagine a balloon. Calling a fxn is like tieing a second string to the balloon and handing the line to the fxn. parameter = new Balloon() will cut that string and create a new balloon (but this has no effect on the original balloon). parameter.pop() will still pop it though because it follows the string to the same, original balloon. Java is pass by value, but the value passed is not deep, it is at the highest level, i.e. a primitive or a pointer. Don't confuse that with a deep pass-by-value where the object is entirely cloned and passed.
dhackner
+2  A: 

To make a long story short, java objects have some very peculiar properties.

In general, java has primitive types (int, bool, char, double, etc) that are passed directly by value. Then java has objects (everything that derives from java.lang.Object). Objects are actually always handled through a reference (a reference being a pointer that you can't touch). That means that in effect, objects are passed by value, as the references are normally not interesting. It does however mean that you cannot change which object is pointed to as the reference itself is passed by value.

Does this sound strange and confusing? Let's consider how C implements pass by reference and pass by value. In C the default convention is pass by value. void foo(int x) passes an int by value. void foo(int *x) is a function that does not want an int a, but a pointer to an int: foo(&a). One would use this with the & operator to pass a variable address.

Take this to C++, and we have references. References are basically (in this context) syntactic sugar that hide the pointer part of the equation: void foo(int &x) is called by foo(a), where the compiler itself knows that it is a reference and the address of the non-reference a should be passed. In java, all variables referring to objects are actually of reference type, in effect forcing call by reference for most intends and purposes without the fine grained control (and complexity) afforded by e.g. C++.

Paul de Vrieze
+3  A: 

The distinction, or perhaps just the way I remember as I used to be under the same impression as the original poster is this: Java is always pass by value. All Objects(in java, anything except for primitives) in java are references. These references are passed by value.

shsteimer
+1  A: 

As many people mentioned it before, Java is always pass-by-value

Here is another example that will help you understand the difference (the classic swap example):

public class Test {
  public static void main(String[] args) {
    Integer a = new Integer(2);
    Integer b = new Integer(3);
    System.out.println("Before: a = " + a + ", b = " + b);
    swap(a,b);
    System.out.println("After: a = " + a + ", b = " + b);
  }

  public static swap(Integer iA, Integer iB) {
    Integer tmp = iA;
    iA = iB;
    iB = tmp;
  }
}

Prints:

Before: a = 2, b = 3
After: a = 2, b = 3

This happens because iA and iB are new local reference variables that have the same value of the passed references (they point to a and b respectively). So, trying to change the references of iA or iB will only change in the local scope and not outside of this method.

pek
+1  A: 

I always think of it as "pass by copy". It is a copy of the value be it primitive or reference. If it is a primitive it is a copy of the bits that are the value and if it is an Object it is a copy of the reference.

public class PassByCopy{
    public static void changeName(Dog d){
        d.name = "Fido";
    }
    public static void main(String[] args){
        Dog d = new Dog("Maxx");
        System.out.println("name= "+ d.name);
        changeName(d);
        System.out.println("name= "+ d.name);
    }
}
class Dog{
    public String name;
    public Dog(String s){
        this.name = s;
    }
}

output of java PassByCopy:

name= Maxx
name= Fido

Primitive wrapper classes and Strings are immutable so any example using those types will not work the same as other types/objects.

SWD
+3  A: 

I have created a thread devoted to these kind of questions for any programming languages here.

Java is also mentioned. Here is the short summary:

  • Java passes it parameters by value
  • "by value" is the only way in java to pass a parameter to a method
  • using methods from the object given as parameter will alter the object as the references point to the original objects. (if that method itself alters some values)
Sven
+78  A: 

Hey there -- just noticed you referenced my article ;)

(http://javadude.com/articles/passbyvalue.htm)

The Java Spec says that everything in java is pass-by-value. There is no such thing as "pass-by-reference" in java.

The key to understanding this is that something like

Dog myDog;

is not a Dog; it's actually a pointer to a Dog.

What that means, is when you have

Dog myDog = new Dog("Rover");
foo(myDog);

you're essentially passing the address of the created Dog object to the foo method.

(I say essentially b/c java pointers aren't direct addresses, but it's easiest to think of them that way)

Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.

if the Method were defined as

public void foo(Dog someDog) {
    someDog.setName("Max");     // AAA
    someDog = new Dog("Fifi");  // BBB
    someDog.setName("Rowlf");   // CCC
}

let's look at what's happening.

  • the parameter someDog is set to the value 42
  • at line "AAA"
    • someDog is followed to the Dog it points to (the Dog object at address 42)
    • that Dog (the one at address 42) is asked to change his name to Max
  • at line "BBB"
    • a new Dog is created. Let's say he's at address 74
    • we assign the parameter someDog to 74
  • at line "CCC"
    • someDog is followed to the Dog it points to (the Dog object at address 74)
    • that Dog (the one at address 74) is asked to change his name to Rowlf
  • then, we return

Now let's think about what happens outside the method:

Did myDog change?

There's the key.

Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it's still pointing to the original Dog.

It's perfectly valid to follow an address and change what's at the end of it; that does not change the variable, however.

Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, you cannot change where that pointer points.

In C++, Ada, Pascal and other languages that support pass-by-reference, you can actually change the variable that was passed.

If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB.

Think of reference parameters as being aliases for the variable passed in. When that alias is assigned, so is the variable that was passed in.

Does that help? (I'll have to add this as an addendum to my article...) -- Scott

Scott Stanchfield
Thanks for that blog entry Scott. It is one of the best explanations on this subject that I've read.
James McMahon
For me, too. Thank you.
Very nice explanation.
Pascal Thivent
This is why the common refrain "Java doesn't have pointers" is so misleading.
Beska
You are wrong, imho. "Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it's still pointing to the original Dog." myDog has value 42 but its name argument now contains "Max", rather than "Rover" on // AAA line.
Comptrol
Think about it this way. Someone has the address of Ann Arbor, MI (my hometown, GO BLUE!) on a slip of paper called "annArborLocation". You copy it down on a piece of paper called "myDestination". You can drive to "myDestination" and plant a tree. You may have changed something about the city at that location, but it doesn't change the LAT/LON that was written on either paper. You can change the LAT/LON on "myDestination" but it doesn't change "annArborLocation". Does that help?
Scott Stanchfield
To the folks who said they like the explanation, thanks for the compliment! My pleasure trying to straighten this all out ;)
Scott Stanchfield
I really needed the AAA, BBB, CCC breakdown and what happens AFTER the method ends. I *think I understand garbage collection a little better now too!
@Scott Stanchfield: I read your article about a year ago and it really helped clear things up for me. Thanks! May I humbly suggest a little addition: you should mention that there is actually a specific term that describes this form of "call by value where the value is a reference" that was invented by Barbara Liskov for describing the evaluation strategy of her CLU language in 1974, in order to avoid confusion such as the one your article addresses: *call by sharing* (sometimes called *call by object-sharing* or simply *call by object*), which pretty much perfectly describes the semantics.
Jörg W Mittag
I don't think we need another term. "Pass-by-value" covers this appropriately. Liskov rocks (I wonder if anyone ever said those exact words to her?), but "call by (object) sharing" adds nothing new. We could add a corresponding "Call by primitive" which also adds nothing new. I like to apply the KISS rule here...
Scott Stanchfield
+1  A: 

Just to show the contrast, compare the following c++ and java snippets:

In C++: Note: Bad code - memory leaks! But it demonstrates the point.

void cppMethod(int val, int &ref, Dog obj, Dog &objRef, Dog *objPtr, Dog *&objPtrRef)
{
    val = 7; // Modifies the copy
    ref = 7; // Modifies the original variable
    obj.SetName("obj"); // Modifies the copy of Dog passed
    objRef.SetName("objRef"); // Modifies the original Dog passed
    objPtr->SetName("objPtr"); // Modifies the original Dog pointed to 
                               // by the copy of the pointer passed.
    objPtr = new Dog("newObjPtr");  // Modifies the copy of the pointer, 
                                   // leaving the original object alone.
    objPtrRef->SetName("objRefPtr"); // Modifies the original Dog pointed to 
                                    // by the original pointer passed. 
    objPtrRef = new Dog("newObjRegPtr"); // Modifies the original pointer passed
}

int main()
{
    int a = 0;
    int b = 0;
    Dog d0 = Dog("d0");
    Dog d1 = Dog("d1");
    Dog *d2 = new Dog("d2");
    Dog *d3 = new Dog("d3");
    cppMethod(a, b, d0, d1, d2, d3);
    // a is still set to 0
    // b is now set to 7
    // d0 still have name "d0"
    // d1 now has name "objRef"
    // d2 now has name "objPtr"
    // d3 now has name "newObjPtrRef"
}

In java,

public static void javaMethod(int val, Dog objPtr)
{
   val = 7; // Modifies the copy
   objPtr.SetName("objPtr") // Modifies the original Dog pointed to 
                            // by the copy of the pointer passed.
   objPtr = new Dog("newObjPtr");  // Modifies the copy of the pointer, 
                                  // leaving the original object alone.
}

public static void main()
{
    int a = 0;
    Dog d0 = new Dog("d0");
    javaMethod(a, d0);
    // a is still set to 0
    // d0 now has name "objPtr"
}

Java only has the two types of passing: by value for built-in types, and by value of the pointer for object types.

Eclipse
just changed one of your references were incorrrect, probably just a typo.
TStamper
A: 

it's a bit hard to understand, but java always copies the value - the point is, normally the value is a reference. therefore you end up with the same object without thinking about it...

Harald Schilly
+1  A: 

The crux of the matter is that the word reference in the expression "pass by reference" means something completely different from the usual mening of the word reference in Java.

Usually in Java reference means a a reference to an object. But the technical terms pass by reference/value from programming language theory is talking about a reference to the memory cell holding the variable, which is someting completely different.

JacquesB
A: 

Primitives are pass by value, objects are a bit more complicated. A reference to the object passed is passed by value, but in most situations, you can treat objects as pass by reference.

+2  A: 

You can never pass by reference in Java, and one of the ways that is obvious is when you want to return more than one value from a method call. Consider the following bit of code in C++:

void getValues(int& arg1, int& arg2) {
    arg1 = 1;
    arg2 = 2;
}
void caller() {
    int x;
    int y;
    getValues(x, y);
    cout << "Result: " << x << " " << y << endl;
}

Sometimes you want to use the same pattern in Java, but you can't; at least not directly. Instead you could do something like this:

void getValues(int[] arg1, int[] arg2) {
    arg1[0] = 1;
    arg2[0] = 2;
}
void caller() {
    int[] x = new int[1];
    int[] y = new int[1];
    getValues(x, y);
    System.out.println("Result: " + x[0] + " " + y[0]);
}

As was explained in previous answers, in Java you're passing a pointer to the array as a value into getValues. That is enough, because the method then modifies the array element, and by convention you're expecting element 0 to contain the return value. Obviously you can do this in other ways, such as structuring your code so this isn't necessary, or constructing a class that can contain the return value or allow it to be set. But the simple pattern available to you in C++ above is not available in Java.

Jared Oberhaus
+3  A: 

As far as I know, Java only knows call by value. This means for primitive datatypes you will work with an copy and for objects you will work with an copy of the reference to the objects. However I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello");
    StringBuffer s2 = new StringBuffer("World");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate Hello World and not World Hello because in the swap function you use copys which have no impact on the references in the main. But if your objects are not immutable you can change it for example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello");
    appendWorld(s);
    System.out.println(s);
}

This will populate Hello World on the command line. If you change StringBuffer into String it will produce just Hello because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World";
}

public static void main(String[] args) {
    String s = new String("Hello");
    appendWorld(s);
    System.out.println(s);
}

However you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello");
    appendWorld(s);
    System.out.println(s.value);
}

edit: i believe this is also the reason to use StringBuffer when it comes to "adding" two Strings because you can modifie the original object which u can't with immutable objects like String is.

kukudas
+1  A: 

You can look at http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

Passing Reference Data Type Arguments

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level. For example, consider a method in an arbitrary class that moves Circle objects:

 public void moveCircle(Circle circle, int deltaX, int deltaY) {
    // code to move origin of circle to x+deltaX, y+deltaY
    circle.setX(circle.getX() + deltaX);
    circle.setY(circle.getY() + deltaY);

    //code to assign a new reference to circle
    circle = new Circle(0, 0);
 }

Let the method be invoked with these arguments:

 moveCircle(myCircle, 23, 56)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

Tamer Erdogan
+2  A: 

A few corrections to some posts.

C does NOT support pass by reference. It is ALWAYS pass by value. C++ does support pass by reference, but is not the default and is quite dangerous.

It doesn't matter what the value is in Java: primitive or address(roughly) of object, it is ALWAYS passed by value.

If a Java object "behaves" like it is being passed by reference, that is a property of mutability and has absolutely nothing to do with passing mechanisms.

I am not sure why this is so confusing, perhaps because so many Java "programmers" are not formally trained, and thus do not understand what is really going on in memory?

Rusty Shackleford
+1. What C *does* support, is *treating* references (which C calls *pointers*) *as* first-class values, and *then* passing them *by value*.
Jörg W Mittag
+1  A: 

Java copies the reference by value. So if you change it to something else (e.g, using new) the reference does not change outside the method. For native types, it is always pass by value.

fastcodejava
A: 

I think anyone that is not confused has not understood the problem fully.

There are two aspects to this:
1) what the language syntax is for passing, manipulating, creating and copying objects.
2) what happens (in memory, on the stack) when parameters are passed and objects are manipulated.

In 1), agreed, there is a difference because the parameter can't be treated as an alias for the object passed as a parameter.
But in 2), I still don't understand what the difference between the two statements "objects are passed by reference" and "objects are passed by value of the reference" should be. In either case, addresses of objects are passed to the function, and of course these addresses are passed by value.
Moreover, there is no difference what you can do with a language that supports "pass by reference" or "pass by value of reference".
Someone suggested the existence of a swap function to be an indicator for that. I disagree with that. The reason is that you cannot in general copy java objects, the assignment Object thisObj = o is a pointer assignment.
But imagine that every Java class had something like a assignment operator for objects in C++, or a method void JavaType.copyFrom(JavaType otherObj) that copied object otherObj into this to be an exact copy (field by field).
In that case, you could write a swap function in java:

void swap(JavaType o1, JavaType o2)
{
  JavaType tmp = new JavaType();
  tmp.copyFrom(o2);
  o2.copyFrom(o1);
  o1.copyFrom(tmp);
}

That is also the reason you can write a swap function with C++ references. Thus, it has nothing to do with parameter passing semantics, but with object assignment semantics.

A: 

yes its pass by referance

Your answer contradicts all the others. You need to give a fuller explanation.
finnw
A: 

Have a look at this code. This code will not throw NullPointerException... It will print "Vinay"

public class Main {
    public static void main(String[] args) {
        String temp = "Vinay";
        print(temp);
        System.err.println(temp);
    }

    private static void print(String temp) {
        temp = null;
    }
}

If Java is pass by reference then it should have thrown NullPointerException as reference is set to Null.

VinAy
The static is throwing it off.public class Main { public static void main(String[] args) { Main m = new Main(); m.aaa(); } public void aaa() { String temp = "Vinay"; print(temp); System.err.println(temp); } private void print(String temp) { temp = null; }}the above code does not NPE.
Milhous
Didn't got what you are trying to say..?
VinAy
+1  A: 

I can't believe that nobody mentioned Barbara Liskov yet. When she designed CLU in 1974, she ran into this same terminology problem, and she invented the term call by sharing (also known as call by object-sharing and call by object) for this specific case of "call by value where the value is a reference".

Jörg W Mittag
I like this distinction in nomenclature. It's unfortunate that Java supports call by sharing for objects, but not call by value (as does C++). Java supports call by value only for primitive data types and not composite data types.
Derek Mahar
I really don't think we needed an extra term - it's simply pass-by-value for a specific type of value. Would adding "call by primitive" add any clarification?
Scott Stanchfield