C# 我可以在Java中通过引用传递参数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1068760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I pass parameters by reference in Java?
提问by ripper234
I'd like semantics similar to C#
's ref
keyword.
我想要类似于C#
'sref
关键字的语义。
采纳答案by oxbow_lakes
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 itselfwhich is passed by value, hence it appearsto be pass-by-reference (and people often claim that it is). This is not the case, as shown by the following:
Java 令人困惑,因为一切都是按值传递的。然而,对于引用类型的参数(即不是原始类型的参数),它是按值传递的引用本身,因此它似乎是按引用传递的(人们经常声称它是)。事实并非如此,如下所示:
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 referenceas follows:
将打印Hello
到控制台。如果您希望打印上述代码,Goodbye
则选项是使用显式引用,如下所示:
AtomicReference<Object> ref = new AtomicReference<Object>("Hello");
mutate(ref);
System.out.println(ref.get()); //Goodbye!
private void mutate(AtomicReference<Object> ref) { ref.set("Goodbye"); }
回答by Marc Gravell
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.
即你的方法改变了它传递的对象的内部状态,然后调用者可以看到它。
回答by dfa
In Java there is nothing at language level similar to ref. In Java there is only passing by value semantic
在 Java 中,没有类似于ref 的语言级别。在 Java中只有通过值语义传递
For the sake of curiosity you can implement a ref-like semantic in Java simply wrapping your objects in a mutable class:
出于好奇,您可以在 Java 中实现类似 ref 的语义,只需将您的对象包装在可变类中:
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"
回答by PeterMmm
Can I pass parameters by reference in Java?
我可以在Java中通过引用传递参数吗?
No.
不。
Why ? Java has only onemode of passing arguments to methods: by value.
为什么 ?Java只有一种向方法传递参数的模式:按值。
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:
这一切都在这张照片中:
回答by Nick Holt
Java is always pass by value.
Java 总是按值传递。
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.
当你传递一个原语时,它是一个值的副本,当你传递一个对象时,它是一个引用指针的副本。
回答by duffymo
From James Gosling in "The Java Programming Language":
来自 James Gosling 在“Java 编程语言”中:
"...There is exactly one parameter passing mode in Java - pass by value - and that keeps things simple. .."
“……Java 中只有一种参数传递模式——按值传递——这让事情变得简单……”
回答by Michal Misiak
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[])
.
另一种选择是使用数组,例如
void method(SomeClass[] v) { v[0] = ...; }
但是 1) 必须在调用方法之前初始化数组,2) 仍然不能以这种方式实现例如交换方法......这种方式在 JDK 中使用,例如在java.util.concurrent.atomic.AtomicMarkableReference.get(boolean[])
.