I'd like semantics similar to C#'s ref keyword.
8 Answers
Java is confusing because everything is passed by value. However for a parameter of reference type (i.e. not a parameter of primitive type) it is the reference itself which is passed by value, hence it appears to be pass-by-reference (and people often claim that it is). This is not the case, as shown by the following:
Object o = "Hello";
mutate(o)
System.out.println(o);
private void mutate(Object o) { o = "Goodbye"; } //NOT THE SAME o!
Will print Hello to the console. The options if you wanted the above code to print Goodbye are to use an explicit reference as follows:
AtomicReference<Object> ref = new AtomicReference<Object>("Hello");
mutate(ref);
System.out.println(ref.get()); //Goodbye!
private void mutate(AtomicReference<Object> ref) { ref.set("Goodbye"); }
-
50Also, an array of length 1 can be used to create a reference if you really want to confuse people :) Jul 1, 2009 at 12:18
-
2AtomicReference and corresponding classes (AtomicInteger) are best for such operations– NaveenJul 13, 2011 at 19:50
-
4AtomicReference is an overkill, you don't necessarily want memory barrier there, and they might cost you. JRuby had perfomance problem because of a volatile variable used when constructing an Object. The option of using array as an ad-hoc reference seems good enough for me, and I've seen it used more than once. Oct 30, 2012 at 14:34
-
I do not think that this is confusing, however this is normal behavior.Value types exist on stack and reference types exist on heap, but reference to for reference types exist on stack also. Thus you always operate with value that is on the stack. I believe that the question here was: Is it possible to pass reference from stack that points on some other value that is also on stack. Or pass reference of other reference which points on some value that lives on heap?– eomeroffNov 21, 2012 at 9:38
-
this i great info. i had been using arrays but this seems like something built for the job. i don't know about performance of this vs. arrays however, if in a performance critical loop– stevehApr 7, 2013 at 5:43
Can I pass parameters by reference in Java?
No.
Why ? Java has only one mode of passing arguments to methods: by value.
Note:
For primitives this is easy to understand: you get a copy of the value.
For all other you get a copy of the reference and this is called also passing by value.
It is all in this picture:

In Java there is nothing at language level similar to ref. In Java there is only passing by value semantic
For the sake of curiosity you can implement a ref-like semantic in Java simply wrapping your objects in a mutable class:
public class Ref<T> {
private T value;
public Ref(T value) {
this.value = value;
}
public T get() {
return value;
}
public void set(T anotherValue) {
value = anotherValue;
}
@Override
public String toString() {
return value.toString();
}
@Override
public boolean equals(Object obj) {
return value.equals(obj);
}
@Override
public int hashCode() {
return value.hashCode();
}
}
testcase:
public void changeRef(Ref<String> ref) {
ref.set("bbb");
}
// ...
Ref<String> ref = new Ref<String>("aaa");
changeRef(ref);
System.out.println(ref); // prints "bbb"
-
1
-
-
why not use
AtomicReference(for non-primitives)? OrAtomicSomething(e.g.:AtomicBoolean) for primitives?– ArvinNov 5, 2013 at 2:39 -
Unfortunately, <T> does not accept primitive types. Would like to do this: Ref<int>– jk7Apr 21, 2017 at 16:05
-
1@jk7 Is it really important enough to have to use
intinstead ofInteger,boolinstead ofBooleanand so on? That is, wrapper classes (which are automatically unwrapped if you worry about syntax) don't work? Dec 30, 2018 at 18:05
From James Gosling in "The Java Programming Language":
"...There is exactly one parameter passing mode in Java - pass by value - and that keeps things simple. .."
-
3The pronouncement of the language god should be declared the answer.– ingyhereOct 25, 2013 at 21:25
-
3@ingyhere :
The pronouncement of the language god should be declared the answer: Not really. Despite what the creator of Java thinks or believe, the absolute answer would come from a quote from the language specification. Feb 5, 2015 at 10:03 -
2In fact it is in the JLS, in section 8.4.1, and the JLS cites Gosling as first author: "When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor."– ingyhereFeb 9, 2015 at 20:53
I don't think you can. Your best option might be to encapsulate the thing you want to pass "by ref" onto another class instance, and pass the (outer) class's reference (by value). If you see what I mean...
i.e. your method changes the internal state of the object it is passed, which is then visible to the caller.
Java is always pass by value.
When you pass a primitive it's a copy of the value, when you pass an object it's a copy of the reference pointer.
Another option is to use an array, e.g.
void method(SomeClass[] v) { v[0] = ...; }
but 1) the array must be initialized before method invoked, 2) still one cannot implement e.g. swap method in this way...
This way is used in JDK, e.g. in java.util.concurrent.atomic.AtomicMarkableReference.get(boolean[]).
Check out my response in: http://stackoverflow.com/a/9324155/1676736
In there I used a simpler version of the wrapper class idea. I don't like setters/getters as a standard. When there is no reason to bury a field I make it 'public'. Especially in something like this.
However, this would work for all but the primitive, or multi-parameter/type returns:
public class Ref<T> {
public T val;
}
Although, I suppose you could just add more type parameters. But I think that creating an inner static class fit-for-purpose would be easier:
public static class MyReturn {
public String name;
public int age;
public double salary;
}
this would be for use when you don't need it for other reasons.
MyReturn mRtn = new MyReturn();
public void myMethod(final MyReturn mRtn){
mRtn.name = "Fred Smith";
mRtn.age = 32;
mRtn.salary = 100000.00;
}
System.out.println(mRtn.name + " " +mRtn.age + ": $" + mRtn.salary);