转载请注明出处
http://blog.csdn.net/pony_maggie/article/details/44120045
作者:小马
看一段代码:
- public class ArrayTest
- {
- //都是引用传递,输出的结果是"goodbbb"
- public void arrayPassTest(String s, String[] ss)
- {
- s = "bad";
- ss[0] = "bbb";
- }
- public static void main(String[] args)
- {
- String s1 = new String("good");
- String[] ss1 = {"aaa"}; //string数组,只有一个元素
- ArrayTest test = new ArrayTest();
- test.arrayPassTest(s1, ss1);
- System.out.println(s1+ss1[0]);
输出结果:
- goodbbb
如果你认为arrayPassTest 函数中,s是作为值传递,而ss是作为引用传递,所以有这样的输出结果,也不算错误,但是决对没有真正理解里面的原因。在这里,String 类型的传递是引用传递,也即是地址传递。这个是毋庸置疑的。因为在java里,String是对象类型,作为参数肯定是引用传递。之所以有值传递的效果,是因为Stirng内部实现时,是用char[] 来存储字符串的,所以String相当于char[]的包装类,那java中,包装类的一个特质就是值操作时体现对应的基本类型的特质,而java中基本数据类型都是值传递。
这个确实有点难理解,尤其是从C/C++转出过的程序员。需要再慢慢揣摩。
ss参数完全符合引用传递的特点,很好理解,不多说。附上String的构造函数实现,
- public String(String original) {
- int size = original.count;
- char[] originalValue = original.value;
- char[] v;
- if (originalValue.length > size) {
- // The array representing the String is bigger than the new
- // String itself. Perhaps this constructor is being called
- // in order to trim the baggage, so make a copy of the array.
- v = new char[size];
- System.arraycopy(originalValue, original.offset, v, 0, size);
- } else {
- // The array representing the String is the same
- // size as the String, so no point in making a copy.
- v = originalValue;
- }
- this.offset = 0;
- this.count = size;
- this.value = v;