按引用传递还是按值传递? [英] Pass by reference or pass by value?

查看:26
本文介绍了按引用传递还是按值传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习一种新的编程语言时,您可能会遇到的一个可能的障碍是该语言是否默认是值传递或引用传递.>

所以这是我问你们所有人的问题,用你们最喜欢的语言,实际上是如何完成的?什么是可能的陷阱?

当然,您最喜欢的语言可以是您玩过的任何语言:
(来源:javaworld.com)

结论或长话短说:

  • Java按值
  • 传递参​​数
  • 按值"是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:

public void swap(int x, int y)
{
  int tmp = x;
  x = y;
  y = tmp;
}

calling this method will result in this:

int pi = 3;
int everything = 42;

swap(pi, everything);

System.out.println("pi: " + pi);
System.out.println("everything: " + everything);

"Output:
pi: 3
everything: 42"

even using 'real' objects will show a similar result:

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"

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)
Original author: Tony Sintes

public 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"

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.


(source: javaworld.com)

Conclusion or a long story short:

  • Java passes it parameters by value
  • "by value" is the only way in java to pass a parameter to a method
  • using methods from the object given as parameter will alter the object as the references point to the original objects. (if that method itself alters some values)

useful links:

这篇关于按引用传递还是按值传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆