我的测试如下:
Node是一个节点类,其有一个Node类型的属性
public class Node {
public Node next;
}
这是主类,其有一个Node属性
public class Test {
public Node node;
public Test(){
node=new Node();
node.next=new Node();
}
public void go(){
this.change(node);
}
public void change(Node node){
node=new Node();
}
}
当我想通过change方法改变node时
测试方法:
public static void main(String[] args) {
Test test=new Test();
System.out.println(test.node);
test.go();
System.out.println(test.node);
}
输出:
test.Node@619a5dff
test.Node@619a5dff
发现node属性并没有改变,但是我如果想改变node.next时
public void change(Node node){
node.next=new Node();
}
测试方法:
public static void main(String[] args) {
Test test=new Test();
System.out.println(test.node.next);
test.go();
System.out.println(test.node.next);
}
输出:
test.Node@619a5dff
test.Node@1ed6993a
却发现node.next改变了,这是为什么啊,java不是值传递吗