原文在此,寫的很是好,解答了個人疑問
http://www.javadude.com/articles/passbyvalue.htmjava
首先放上一段代碼,我是在找尋這段代碼的內部原理的時候,在stackoverflow上看到了上面那篇文章做者的答案,因而閱讀一番,受益不淺this
package learnJava; class A{ int value; public A(int value) { this.value = value; } public void setValue(int value) { this.value = value; } public int getValue() { return value; } } public class TestClass { public static void changeValue(A a, A b){ //這只是改變了指向Object的局部變量指針的值,因此method外面是不會改變的 a = b; } public static void changeValue2(A a){ //這裏是對局部變量的指針指向的Object進行set操做,因此method外也會改變 int v = a.getValue(); v++; a.setValue(v); } public static void main(String[] args) { A a1 = new A(3); A a2 = new A(8); System.out.println(a1.value); // 3 changeValue(a1, a2); System.out.println(a1.value); // 3 changeValue2(a1); System.out.println(a1.value); // 4 } }