class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode() { } public TreeNode(int val) { this.val = val; } } public class Solution { public static void main(String[] args) { TreeNode t1 = null; change(t1); System.out.println(t1==null?null:t1.val); TreeNode t2 = new TreeNode(100); Change(t2); System.out.println(t2.val); } //无论传递的参数是否是null,都无法在函数内部改变引用变量的指向! public static void change(TreeNode t1){ t1 = new TreeNode(100); } public static void Change(TreeNode t2){
t1 = new TreeNode(200);
}
}