1.测试代码:传递String
public class MyProject
{
public static void main(String[] agrs) {
String name = "haha";
System.out.println("before change : " + name);
change(name);
System.out.println("after change : " + name);
}
private static void change(String s) {
s = "changed";
}
}
测试结果
before change : haha
after change : haha
2.测试代码:传递对象-修改对象信息
public class MyProject
{
public static void main(String[] agrs) {
Student mStudent = new Student("xiaohong", 10);
System.out.println("before change : " + mStudent.getName() + " " + mStudent.getAge());
change(mStudent);
System.out.println("after change : " + mStudent.getName() + " " + mStudent.getAge());
}
private static void change(Student s) {
s.setName("xiaoming");
s.setAge(15);
}
}
测试结果
before change : xiaohong 10
after change : xiaoming 15
3.测试代码:传递对象-修改对象指向
public class MyProject
{
public static void main(String[] agrs) {
Student mStudent = new Student("xiaohong", 10);
System.out.println("before change : " + mStudent.getName() + " " + mStudent.getAge());
change(mStudent);
System.out.println("after change : " + mStudent.getName() + " " + mStudent.getAge());
}
private static void change(Student s) {
s = new Student("xiaoming", 15);
}
}
测试结果
before change : xiaohong 10
after change : xiaohong 10