java为什么把一个对象传入方法中把原有对象的值改变 但是地址不会变呢
已经了解是值传递,将对象拷贝一份传入方法,在改变了对象的值基础上 原有对象不变,但是为什么值都改变了,但是对象的地址确实一模一样? 求大神解答
public class Test1 {
static class Student{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Student student = new Student();
student.setName("张三");
System.out.println("原对象内存地址:"+student+",值:"+student.getName());
Student newStudent = what(student);
System.out.println("新对象内存地址:"+newStudent+",值:"+student.getName());
System.out.println(student==newStudent);
}
public static Student what(Student student){
student.setName("李四");
return student;
}
}