1

Possible Duplicates:
What's the difference between passing by reference vs. passing by value?
Java and C++ pass by value and pass by reference

Java is "pass by value" or "pass by reference"?

and what about c++ and c? is c++ and c "passed by value" or "passed by reference"?

and what is the difference between "pass by value" or "pass by reference"?

Community
  • 1
  • 1

3 Answers3

6

Java is pass by value, but the value you pass may be a reference.

In C++, you choose whether to pass by value or by reference. In C, you always pass by value, and there is no such thing as a reference; you have to pass a pointer to do something similar.

If you pass by value, the function gets a copy of the variable you pass. If you change the copy, the original value remains the same (though, it the value passed was itself a reference, you may change the referred object).

If you pass by reference, the function gets the actual variable passed. If the function changes that variable, you are actually changing the variable that was passed to the function.

Gorpik
  • 10,940
  • 4
  • 36
  • 56
3

Java is "pass by value." Always.

C++ allows pass by reference. Everything in C is passed by value.

When you pass a variable by reference to a function, the function can change the value of the variable in the caller. When you pass by value, the function gets a copy of the variable.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 3
    Just want emphasize that "pass by value" in java with referenced objects means that the **Reference** is passed by value. Effectively it is actually passed by reference. – Yochai Timmer Jun 17 '11 at 21:22
  • 1
    *Effectively it is actually passed by reference.* -- Naah, the right thing to say is probably that the object isn't passed at all. – aioobe Jun 17 '11 at 21:24
-4

Java is pass by value for basic types (int, double), and pass by reference for any class inheriting from Object.

C++ is pass however you like.

pass by value makes a copy of the parameter, so any changes made in the function only have an effect inside that function.

pass by reference doesn't copy, so any changes made to that parameter within the subfunction will effect the value outside of that function.

totowtwo
  • 2,101
  • 1
  • 14
  • 21
  • +0: Java is always pass by value. References can be passed by value. ;) – Peter Lawrey Jun 17 '11 at 21:21
  • 1
    "*and pass by reference for any class inheriting from Object.*". -- Wrong. It's pass by value for references too. – aioobe Jun 17 '11 at 21:21
  • 3
    I'm sorry but this isn't true. The reference is passed by value, so any methods called on the object passed will use the same object reference, but if you change the reference of the parameter in the function, that change won't reflect back to the caller. So `param.setBar()` is fine, `param = new Foo()` will have no visible effect from the outside. – biziclop Jun 17 '11 at 21:29
  • Pass by value does indeed make a copy of the argument. In Java, when the parameter in Java is a reference type, a copy of the argument is made. That argument is a reference, not the object. Changing the parameter (to another reference) doesn't affect the caller. – Jon Skeet Jun 17 '11 at 21:30