随便写写留着自己看。
首先,Java的参数传递,不管是基本数据类型还是引用类型的参数,都是按值传递,没有按引用传递!
当一个实例对象作为参数被传递到方法中时,参数的值就是该对象的引用的一个副本。指向同一个对象,对象的内容可以在被调用的方法内改变,但对象的引用(不是引用的副本) 是永远不会改变的。
下面几个例子帮助理解。
例1:
先看一段代码:
1 public class Test2 { 2 String str = new String("good"); 3 String str1 = "wtf"; 4 int a = 10; 5 char[] ch = { 'a', 'b', 'c' }; 6 7 public static void main(String[] args) { 8 Test2 test2 = new Test2(); 9 test2.change(test2.str, test2.str1, test2.ch, test2.a); 10 System.out.println(test2.str); 11 System.out.println(test2.str1); 12 System.out.println(test2.a); 13 System.out.println(test2.ch); 14 } 15 16 public void change(String str, String str1, char[] ch, int a) { 17 str = new String("change ok"); 18 str1 = "change ok"; 19 ch[0] = 'g'; 20 a = 13; 21 } 22 }
1 good 2 wtf 3 10 4 gbc