JAVA中的参数按值传递与按引用传递
天飞 2013-03-20 12:19:00 浏览13 评论0java c++ string c# static class void test
摘要: 看着有点恼火,你说为毛JAVA不用C或C++一样的指针传递呢? C#和JAVA都号称取消了指针,但用户在编程时,不一样面对和学C/C++一样的变量及变量指针地址的问题么? 代码分三个: 1 public class TempTest{ 2 private void test1(int a) { 3 a++; 4 System.
看着有点恼火,你说为毛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 }
结果分三段:

用云栖社区APP,舒服~
【云栖快讯】红轴机械键盘、无线鼠标等753个大奖,先到先得,云栖社区首届博主招募大赛9月21日-11月20日限时开启,为你再添一个高端技术交流场所 详情请点击 评论文章 (0) (0) (0)