1. 关于JAVA中参数传递问题有两种,一种是按值传递(如果是基本类型),另一种是按引用传递(如果是對象). 
  2.  
  3. 首先以两个例子开始: 
  4.  
  5. 1) 
  6. public class Test2 { 
  7.  
  8. public static void main (String [] args) { 
  9. StringBuffer a = new StringBuffer ("A"); 
  10. StringBuffer b = new StringBuffer ("B"); 
  11. operate (a,b); 
  12. System.out.println(a+","+b); 
  13.  
  14. static void operate(StringBuffer x, StringBuffer y){ 
  15. x.append(y); 
  16. y = x; 
  17. 输出:AB,B 
  18.  
  19. 2) 
  20. public class Test2 { 
  21.  
  22. public static void add3 (Integer i){ 
  23. int val=i.intValue(); 
  24. val += 3; 
  25. i = new Integer (val); 
  26.  
  27. public static void main (String args [ ] ) { 
  28. Integer i = new Integer (0); 
  29. add3 (i); 
  30. System.out.println (i.intValue ( )); 
  31. 输出:0 
  32.  
  33. 首先我们应该明白JAVA中的参数传递全是以值传递的。是基本类型,就拷贝一个基本类型传进方法;是引用,就拷贝一个引用变量传进去方法,理解了这两点就能理解方法操作对象的相关问题了。最好能画出引用指向对象的图出来,就能完全理解了。 
  34. 第1题,调用operate方法时,传入了两个引用a,b的拷贝x,y,这两个x,y都指向原a,b引用所指向的对象。x.append(y)对它指向的 对象(即a指向的对象)进行了操作。而x=y,只是两个拷贝变量在赋值,并没有影响到原b所指向的对象。所以b所指向的对象仍然为B。 
  35. 第2题,i=new Integer(val)只是一个引用的拷贝指向了另外一个对象,而原来的i仍然是指向对象new Integer(0)的。 
  36. 把握住了JAVA都是传值并且传的都是拷贝的话,类似的题大家都能迎刃而解了。 
  37.  
  38. Java中的参数传递只有一种方式: by value. 理论说教太麻烦了,直接看些例子吧: 
  39. 1). 基本类型 
  40. public class A{ 
  41. public static void main(String[] args){ 
  42. int x = 1; 
  43. System.out.println(x);    //1 
  44. test(x); 
  45. System.out.println(x);    //还是1==>By value 
  46.  
  47. static void test(int a){ 
  48. a = 2; 
  49.  
  50. 2). 引用类型 
  51. public class B{ 
  52. public static void main(String[] args){ 
  53. Integer x = new Integer(1); 
  54. System.out.println(x); 
  55. test(x); 
  56. System.out.println(x); 
  57.  
  58.  
  59. static void test(Integer a){ 
  60. a = new Integer(2); 
  61.  
  62. 理解这里的关键是区分对象和引用。 这里声明的x是一个引用,而不是一个对象(只是Java把它设计为看上去好像是对象一样)。这个引用它指向了一个对象,这个对象就是后面用new关键字生成的对象。因此,可以说x指向了一个Integer对象。 
  63. 在调用test方法的时候,程序将x作为参数传递给test方法了。这里仍然是值传递,在test调用过程中,会产生一份新的引用(不妨叫做y)。此时,x和y指向了同一个对象。 
  64. x和y指向的是同一个对象, 由于Java的设计,我们可以通过操作引用来达到操作对象的目的。因此,如果我们此时使用y来修改对象的属性 (例如,y.someField++); 你可以看到x指向的对象同时也被修改到了。 
  65. 另一方面,如果我们让y指向另外一个对象, y=new Integer(2); 此时x和y就指向了不同的 
  66. 对象。y修改了它指向的对象的属性,很显然不会影响到x指向的对象。 
  67.  
  68. 有人说了数组。数组也是一个引用类型,它的参数传递方式按照引用类型的参数传递一样可以解释得通: 
  69.  
  70. import java.util.Arrays; 
  71.  
  72. public class A{ 
  73. public static void main(String[] args){ 
  74. int[] aa = {3, 2, 1}; 
  75. System.out.println(Arrays.toString(aa)); //[3, 2, 1] 
  76. test(aa); 
  77. System.out.println(Arrays.toString(aa)); //[3, 2, 1] 
  78. test2(aa); 
  79. System.out.println(Arrays.toString(aa)); //[4, 2, 1] 
  80.  
  81. static void test(int[] a){ 
  82. a = new int[]{1, 2, 3};   //指向了新对象 
  83.  
  84. static void test2(int[] a){ 
  85. if(a != null && a.length > 0) 
  86. a[0]++;              //修改原来的那个对象 
  87.  
  88. 对象是传引用,简单类型是传值,不要被网上的一些概念所迷惑!!!你可以自己做个试验。 
  89. 至于String等类型传的还是引用。如果你用concat方法,String对象的原值就会被改变。 
  90. 但你如果按如下方法: 
  91. public class Test { 
  92. public static void test(String str) { 
  93. str = "World"
  94. public static void main(String[] args) { 
  95. String string = "Hello"
  96. test(string); 
  97. System.out.println(string); 
  98.  
  99.   运行结果:Hello 
  100. 这里str = "World" 就等同于 String str=new String("World")。所以结果没有改变!!! 
  101. 下列程序在1处是否会有异常,如果没有,输出是什么?是否会运行到2处,如果会,输出是什么?为什么会有这样的结果? 
  102.  
  103.   import java.util.arraylist; 
  104. import java.util.list; 
  105.  
  106. public class testclass { 
  107.  
  108.  
  109. public static void main(string args[]) { 
  110. list list = new arraylist(); 
  111. test2(list); 
  112. system.out.println(list.size()); // 1处 
  113. test3(list); 
  114. system.out.println(list.size()); // 2处 
  115.  
  116. public static void test2(list list) { 
  117. list = null
  118.  
  119. public static void test3(list list) { 
  120. list.add(“aaaa“); 
  121.  
  122. plumechen: 
  123.  
  124. 不会出错的。结果是0,1。 
  125.  
  126. 因为test2(list)传得是list的引用,我理解成指针置的副本,list=null;只是把那个传入的值设置为null,不改变原来 list的指针和内容。test3(list)传入的一样,但是执行了list.add()由于传入指针值的副本也指向原来的那个list的地址,所以原 来的那个list的内容就改变了,size变成了1了