java方法传递参数传递的到底是什么?值调用:引用调用

来源:互联网 发布:编程语言效率排行 编辑:程序博客网 时间:2024/05/17 22:46

前言:在Java中我们常常会编写很多方法,大多数方法都会传递参数,那这参数传递的究竟是什么呢?是按值调用还是按引用调用?下面我们就来详细讨论下这个问题。

1.传递基本的数据类型:其实在java中总是采用的是按值调用,也就是说,方法得到的并不是传入参数的本身,而是传入参数的一个拷贝。方法并不能修改传递给它任何参数变量的内容。

╰(*°▽°*)╯举个栗子:

package com.zrh.test;public class Student {private int sid;private String name;private double score;private int age;public static void addScore(double score){score = score+score;System.out.println("addScore_score:"+score);}public static void main(String[] args) {double score = 50;Student.addScore(score);System.out.println("main_score:"+score);}}

结果:

addScore_score:100.0main_score:50.0

结果分析:一个学生的分数是50分,调用addScore方法将分数翻倍,从打印出的信息可以看出addScore方法并没有改变传入的参数score的值,因为addScore实际传递的只是参数score的一个拷贝值,传入的参数score的作用域只在addScore方法内,方法执行完就被销毁了,对传递给方法的参数变量没有任何作用。


2.传递引用:传递的是对象引用的拷贝,拷贝变量和传入变量指向的是同一个对象,所以传递引用可以修改对象的参数。

╰(*°▽°*)╯举个栗子:

public class Student {private int sid;private String name;private double score;private int age;private static int nextId = 0;public Student() {nextId++;this.sid = nextId;}@Overridepublic String toString() {return "Student [sid=" + sid + ", name=" + name + ", score=" + score+ ", age=" + age + "]";}public static void addScore(double score){score = score+score;System.out.println("addScore_score:"+score);}public static void editStudent(Student student){student.name = "jack";student.age = 15;}public static void swap(Student student1,Student student2){Student temp = student1;student1 = student2;student2 = student1;}public static void main(String[] args) {Student student = new Student();editStudent(student);System.out.println(student);}}
运行结果:因为拷贝的引用变量和传入方法的引用变量指向同一个存储对象,所以在方法editStudent中修改了对象的参数,也就就是修改了传入的引用变量所指向对象的参数。
Student [sid=1, name=jack, score=0.0, age=15]

3.需要注意的问题:在很多时候我们都以为java对对象采用的是引用的调用,实际上是不对的,其实调用的是拷贝的引用。

╰(*°▽°*)╯举个栗子:

public class Student {private int sid;private String name;private double score;private int age;private static int nextId = 0;public Student() {nextId++;this.sid = nextId;}@Overridepublic String toString() {return "Student [sid=" + sid + ", name=" + name + ", score=" + score+ ", age=" + age + "]";}public static void addScore(double score){score = score+score;System.out.println("addScore_score:"+score);}public static void editStudent(Student student){student.name = "jack";student.age = 15;}public static void swap(Student student1,Student student2){Student temp = student1;student1 = student2;student2 = student1;}public static void main(String[] args) {Student student1 = new Student();Student student2 = new Student();student1.name = "syudent1";student1.age = 15;student2.name = "syudent2";student2.age = 18;swap(student1, student2);System.out.println("student1:"+student1);System.out.println("student2:"+student2);}}
运行结果:student1和student2并没有交换对象,因为方法传递的是拷贝引用,在方法内引用交换了互相指向的对象,但是方法结束后两个变量就销毁了,并没有改变main中student1和student2引用的对象。
student1:Student [sid=2, name=syudent1, score=0.0, age=15]student2:Student [sid=3, name=syudent2, score=0.0, age=18]

阅读全文
0 0
原创粉丝点击