通过引用还是通过值? [英] Pass by reference or pass by value?
问题描述
学习一种新的编程语言时,您可能遇到的障碍之一是该语言默认情况下是按值传递还是按引用传递. >
这是我向大家提出的问题,用您喜欢的语言如何实际完成了吗?什么是可能的陷阱?
您最喜欢的语言当然可以是您曾经玩过的任何语言:受欢迎的 ,晦涩,新,旧 ...
这是我对 即使使用真实"对象也会显示类似的结果: 因此,很明显,Java通过按值传递其参数,作为 pi 和 everything 的值以及 MyObj对象不会交换.
请注意,按值"是java中将参数传递给方法的唯一方法". (例如,像c ++这样的语言允许开发人员在参数类型之后使用'& '通过引用来传递参数) 现在是棘手的部分,或者至少是会使大多数新的Java开发人员感到困惑的部分:(从 tricky 成功更改了pnt1的值!
这意味着对象是通过引用传递的,事实并非如此!
正确的说法是: 对象引用通过值传递. 来自Tony Sintes的更多信息: 该方法成功更改了
pnt1的值,即使它是
按价值传递;但是,交换
pnt1和pnt2失败!这是主要的
混乱的根源.在main()中
方法,pnt1和pnt2仅此而已
比对象引用.当你通过时
pnt1和pnt2到ricky()方法,
Java按值传递引用
就像其他任何参数一样.这
表示传递给
方法实际上是
原始参考.图1
显示了两个指向
Java传递一个相同的对象后,
反对一种方法. 结论或简短的故事: 有用的链接: When learning a new programming language, one of the possible roadblocks you might encounter is the question whether the language is, by default, pass-by-value or pass-by-reference. So here is my question to all of you, in your favorite language, how is it actually done? And what are the possible pitfalls? Your favorite language can, of course, be anything you have ever played with: popular, obscure, esoteric, new, old... Here is my own contribution for the Java programming language. first some code: calling this method will result in this: even using 'real' objects will show a similar result: thus it is clear that Java passes its parameters by value, as the value for pi and everything and the MyObj objects aren't swapped.
be aware that "by value" is the only way in java to pass parameters to a method. (for example a language like c++ allows the developer to pass a parameter by reference using '&' after the parameter's type) now the tricky part, or at least the part that will confuse most of the new java developers: (borrowed from javaworld) tricky successfully changes the value of pnt1!
This would imply that Objects are passed by reference, this is not the case!
A correct statement would be: the Object references are passed by value. more from Tony Sintes: The method successfully alters the
value of pnt1, even though it is
passed by value; however, a swap of
pnt1 and pnt2 fails! This is the major
source of confusion. In the main()
method, pnt1 and pnt2 are nothing more
than object references. When you pass
pnt1 and pnt2 to the tricky() method,
Java passes the references by value
just like any other parameter. This
means the references passed to the
method are actually copies of the
original references. Figure 1 below
shows two references pointing to the
same object after Java passes an
object to a method. Conclusion or a long story short: useful links: 这篇关于通过引用还是通过值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!public class MyObj {
private String msg;
private int number;
//getters and setters
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
//constructor
public MyObj(String msg, int number) {
setMsg(msg);
setNumber(number);
}
}
public static void swap(MyObj x, MyObj y)
{
MyObj tmp = x;
x = y;
y = tmp;
}
public static void main(String args[]) {
MyObj x = new MyObj("Hello world", 1);
MyObj y = new MyObj("Goodbye Cruel World", -1);
swap(x, y);
System.out.println(x.getMsg() + " -- "+ x.getNumber());
System.out.println(y.getMsg() + " -- "+ y.getNumber());
}
"Output:
Hello world -- 1
Goodbye Cruel World -- -1"
(来源: javaworld. com )
public void swap(int x, int y)
{
int tmp = x;
x = y;
y = tmp;
}
int pi = 3;
int everything = 42;
swap(pi, everything);
System.out.println("pi: " + pi);
System.out.println("everything: " + everything);
"Output:
pi: 3
everything: 42"
public class MyObj {
private String msg;
private int number;
//getters and setters
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
//constructor
public MyObj(String msg, int number) {
setMsg(msg);
setNumber(number);
}
}
public static void swap(MyObj x, MyObj y)
{
MyObj tmp = x;
x = y;
y = tmp;
}
public static void main(String args[]) {
MyObj x = new MyObj("Hello world", 1);
MyObj y = new MyObj("Goodbye Cruel World", -1);
swap(x, y);
System.out.println(x.getMsg() + " -- "+ x.getNumber());
System.out.println(y.getMsg() + " -- "+ y.getNumber());
}
"Output:
Hello world -- 1
Goodbye Cruel World -- -1"
Original author: Tony Sintespublic void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
tricky(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}
"Output
X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0"
(source: javaworld.com)