补充:按值引用:表示方法接收的是调用者提供的值;
按引用传递:表示方法接收的是调用者提供的变量地址;
分析: 基本数据类型: @org.junit.Test
public void test12() {
int result =10;
add(result);
System.out.println(result);
}
public void add(int a){
a=a+5;
}
引用类型:
@org.junit.Test
public void test13() {
Student student = new Student();
student.setName("TEST");
student.setSalary(100);
student.addSalary(student);
System.out.println(student.getSalary());
}
public class Student {
public String name;
private int age;
private int salary;
public void addSalary(Student student){
this.salary =student.salary*3;
}
}
@org.junit.Test
public void test13() {
Student student = new Student();
student.setName("TEST");
student.setSalary(100);
Student student1 = new Student();
student.setName("TEST1");
student.setSalary(100);
System.out.println("交换前:student:"+student.hashCode()+",student1:"+student1.hashCode());
swap(student,student1);
System.out.println("交换后:student:"+student.hashCode()+",student1:"+student1.hashCode());
}
public void swap(Student s,Student s1){
System.out.println("交换前:s:"+s.hashCode()+",s1:"+s1.hashCode());
Student temp =s;
s=s1;
s1=temp;
System.out.println("交换后:s:"+s.hashCode()+",s1:"+s1.hashCode());
}



