1、方法傳遞的是基本數(shù)據(jù)類型
public class SortMain {
public static void main(String[] args) {
int i=5;
changei(i);
System.out.println("after="+i);
}
}
private static void changei(int i) {
i=i*3;
System.out.println("before="+i);
}
打印結(jié)果
before=15
after=5
2、方法傳遞的是對象揩抡,修改的是對象數(shù)據(jù)和對象引用的不同
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class SortMain {
public static void main(String[] args) {
Student student = new Student("小馬哥", 25);
changeName(student);
System.out.println(student.toString());
Student refre = new Student("引用", 28);
changeStu(refre);
System.out.println(refre.toString());
}
private static void changeStu(Student refre) {
refre = new Student("引用修改", 30);
System.out.println(refre.toString());
}
private static void changeName(Student student) {
student.setName("修改名稱");
System.out.println(student.toString());
}
}
打印結(jié)果:
Student{name='修改名稱', age=25}
Student{name='修改名稱', age=25}
Student{name='引用修改', age=30}
Student{name='引用', age=28}