天天看点

值传递和引用传递的区别

值传递:方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际参数的值。

引用传递:也称为传地址。方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对应的形式参数,在方法执行中,对形式参数的操作实际上就是对实际参数的操作,方法执行中形式参数值的改变将会影响实际参数的值。

而在JAVA中只有值传递,基本类型传递的是值的副本,引用类型传递(不是上面说的引用传递)的是引用的副本。下面来看几个例子:

1.在函数中传递基本数据类型

复制代码

1 public class TestValue {

2 public static void main(String[] args) {

3 int i = 3;

4 int j = 4;

5 change(i,j);

6 System.out.println(i);

7 System.out.println(j);

8 }

9 public static void change(int i,int j){

10 int temp = i;

11 i = j;

12 j = temp;

13 }

14 }

复制代码

结果显示i和j并没有发生交换。因为参数中传递的是基本数据类型i和j的副本,在函数中交换的也是副本的值而不是数据本身!

2.在函数中传递引用数据类型

复制代码

1 public class TestValue1 {

2 public static void change(int[] count){

3 count[0] =10;

4 }

5 public static void main(String[] args) {

6 int[] count = {1,2,3,4,5};

7 change(count);

8 System.out.println(count[0]);

9 }

10 }

复制代码

结果count[0]发生了变化,等于10。在方法中传递的是int数组,实际上传递的是其引用count的副本,他们都指向数组对象,即对备份所调用的方法更改的是同一个对象。

3.重点来看看下面这个例子(转)

复制代码

class Student{

  private float score;

  public Student(float score) {

this.score = score;

}

public void setScore(float score) {

this.score = score;

}

public float getScore() {

return score;

}

}

public class ParamTest {

public static void main(String[] args) {

Student a = new Student(0);

Student b = new Student(100);

System.out.println(“交换前:”);

System.out.println(“a的分数:” + a.getScore() + “— b的分数:” + b.getScore());

swap(a, b);

    System.out.println("交换后:");
    System.out.println("a的分数:" + a.getScore() + "--- b的分数:" + b.getScore());
}
public static void swap(Student x, Student y) {
    Student temp = x;
    x = y;
    y = temp;
}
           

}

复制代码

【运行结果】:交换前:a的分数:0.0— b的分数:100.0

交换后:a的分数:0.0— b的分数:100.0

可以看出,两者并没有实现交换。这是为何?接下来一步一步看看swap调用的过程:

将对象a,b的拷贝(还是值传递)分别赋值给x,y,此时a和x指向同一对象,b和y指向同一对象

swap方法体完成x,y的的交换,此时a,b并没有变化

方法执行完成,x和y不再使用,a依旧指向new Student(0),b指向new Student(100)

本质是a和b分别指向了两个不同的堆内存空间。