The x and y variables exist in the stack, pointing to the two objects in the heap, new Point(0,0) and new Point(1,1) respectively. Execute x = y,x to point the reference to the address in y memory, x .setLocation(5,5), directly modifies y. Finally, both x and y point to y in the heap, so what is printed is the modified content of x, x=5, y=5
Question to think about: What is the final output?
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
User u = new User("张三");
changeUser(u);
System.out.println(u.getName());
}
public static void changeUser(User user){
user.setName("李四");
user = new User("王五");
user.setName("赵六");
}
}
The x and y variables exist in the stack, pointing to the two objects in the heap, new Point(0,0) and new Point(1,1) respectively. Execute x = y,x to point the reference to the address in y memory, x .setLocation(5,5), directly modifies y. Finally, both x and y point to y in the heap, so what is printed is the modified content of x, x=5, y=5
google pass by value and pass by reference
Question to think about: What is the final output?
That is, all you quoted are the space addresses of y