I guess java does not have the option "pass by reference" .But why ? Because sometimes it is very needed.
-
Java, like C, is purely a pass by value language. You can pass references, but you must pass the references by value. It's hard to imagine why this is a problem. Why do you need to pass values by reference when you can just pass references by value and get, more or less, the same effect? – David Schwartz Apr 15 '12 at 08:49
-
I don't think that superuser is the right place to discuss language concepts. – Baarn Apr 15 '12 at 08:50
-
And by the way, everything in java gets passed by reference, except atomic types. And there are wrapper classes for each of them. – Baarn Apr 15 '12 at 08:53
-
@WalterMaier-Murdnelch Nothing in Java is passed by reference ever. If you think Java always passes by reference, try this function: `void swap(Foo x, Foo y) { Foo temp=x; x=y; y=temp; }` and watch it fail. While `x` and `y` *are* references, they are *passed* by value. – David Schwartz Apr 15 '12 at 09:31
-
True, I confused it a bit. I now just wonder if there is any language that passes by reference, pointers (in C) are also just a reference thats getting passed as a value. – Baarn Apr 15 '12 at 10:13
-
I really didn't want to migrate this, but there was no other option, sorry. – slhck Apr 15 '12 at 10:41
-
Can you provide an example, because for every situation where pass by reference is used in other languages, you can write much the same thing in Java. i.e. there are very few case where you can't work around this. – Peter Lawrey Apr 15 '12 at 10:50
-
@WalterMaier-Murdnelch C++ has pass by reference. This will work: `void IncrementCallersFoo(Foo& j) { j++; }` (because `j` is passed by reference) but this will not: `void IncrementCallersFoo(Foo j) { j++; }` (because `j` is passed by value). – David Schwartz Apr 15 '12 at 11:07
-
@DavidSchwartz: Thanks for the example, I don't want to start nitpicking, but somehow for me that just moved the deferencing to the parameter. consider (pseudo java) `void inc(Integer foo) { foo.val++; };` – Baarn Apr 15 '12 at 11:19
-
I agree. Passing a reference by value and passing a value by reference are very similar. That's why you can have a language that only permits one type of parameter passing and it doesn't limit you very much. (In fact, one disadvantage is that in C++, if you see `foo(bar);` you may be passing `bar` by value or by reference. You can only tell by looking elsewhere. You don't have that issue in Java.) – David Schwartz Apr 15 '12 at 11:25
-
Because there isn't. Ask Jim Gosling. Question is not constructive. – user207421 Apr 15 '12 at 22:52
3 Answers
Any time you feel the need to pass a value by reference, instead pass a reference by value. While Java does not have a "pass by reference" mechanism, it does have references. So just pass those references by value.

- 179,497
- 17
- 214
- 278
I guess the only place you really need pass by reference is to return multiple values from a function like the out
keyword in C. To do this in Java, you can either create a class to hold the objects or use an existing Pair class offered by many libraries.
Similarly, you can pass a stub object that refers to something else and then you can change the reference in the object and see it reflected externally:
public static class Holder<T> {
public T value;
public Holder(T value) {
this.value = value;
}
}
private Holder<Object> itsValueCanBeSeenAsPassedByReferencce
= new Holder<Object>("a");
public void doSomething(Holder<Object> holder) {
holder.value = "b";
}

- 7,187
- 3
- 39
- 79
In Java no matter what type of argument you pass the corresponding parameter (primitive variable or object reference) will get a copy of that data, which is exactly how pass-by-value (i.e. copy-by-value) works. In Java, if a calling method passes a reference of an object as an argument to the called method then the passed in reference gets copied first and then passed to the called method. Both the original reference that was passed-in and the copied reference will be pointing to the same object. So no matter which reference you use, you will be always modifying the same original object, which is how the pass-by-reference works as well.

- 81
- 1
- 6