看着有点恼火,你说为毛JAVA不用C或C++一样的指针传递呢?
C#和JAVA都号称取消了指针,但用户在编程时,不一样面对和学C/C++一样的变量及变量指针地址的问题么?
代码分三个:
1 public class TempTest{ 2 private void test1(int a) { 3 a++; 4 System.out.println("The char A in test1 method value is :" + a); 5 } 6 7 public static void main(String[] args) { 8 TempTest t = new TempTest(); 9 int a = 3;10 t.test1(a);11 System.out.println("The char A in main method value is :" + a);12 }13 }
1 public class TempTest1 { 2 private void test1(A a) { 3 a.age = 20; 4 System.out.println("The age in test1 method value is :" + a.age); 5 } 6 7 public static void main(String[] args) { 8 TempTest1 t = new TempTest1(); 9 A a = new A();10 a.age = 10;11 t.test1(a);12 System.out.println("The age in main method value is :" + a.age);13 }14 }15 class A {16 public int age = 0;17 }
1 public class TempTest2 { 2 private void test2(A a) { 3 a = new A(); 4 a.age = 20; 5 System.out.println("The age in test2 method value is :" + a.age); 6 } 7 8 public static void main(String[] args) { 9 TempTest2 t = new TempTest2();10 A a = new A();11 a.age = 10;12 t.test2(a);13 System.out.println("The age in main method value is :" + a.age);14 }15 }16 class A {17 public int age = 0;18 }
结果分三段: