java的String类型作为参数传递

转载:http://blog.csdn.net/pony_maggie/article/details/44120045

看一段代码:


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class ArrayTest   
  2. {  
  3.     //都是引用传递,输出的结果是"goodbbb"  
  4.     public void arrayPassTest(String s, String[] ss)  
  5.     {  
  6.         s = "bad";  
  7.         ss[0] = "bbb";  
  8.     }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void main(String[] args)   
  2.     {  
  3.         String s1 = new String("good");  
  4.         String[] ss1 = {"aaa"}; //string数组,只有一个元素  
  5.           
  6.         ArrayTest test = new ArrayTest();  
  7.         test.arrayPassTest(s1, ss1);  
  8.         System.out.println(s1+ss1[0]);  

输出结果:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. goodbbb  


如果你认为arrayPassTest 函数中,s是作为值传递,而ss是作为引用传递,所以有这样的输出结果,也不算错误,但是决对没有真正理解里面的原因。在这里,String 类型的传递是引用传递,也即是地址传递。这个是毋庸置疑的。因为在Java里,String是对象类型,作为参数肯定是引用传递。之所以有值传递的效果,是因为Stirng内部实现时,是用char[] 来存储字符串的,所以String相当于char[]的包装类,那java中,包装类的一个特质就是值操作时体现对应的基本类型的特质。


这个确实有点难理解,尤其是从C/C++转出过的程序员。需要再慢慢揣摩。


ss参数完全符合引用传递的特点,很好理解,不多说。附上String的构造函数实现,

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public String(String original) {  
  2.     int size = original.count;  
  3.     char[] originalValue = original.value;  
  4.     char[] v;  
  5.     if (originalValue.length > size) {  
  6.         // The array representing the String is bigger than the new  
  7.         // String itself.  Perhaps this constructor is being called  
  8.         // in order to trim the baggage, so make a copy of the array.  
  9.         v = new char[size];  
  10.         System.arraycopy(originalValue, original.offset, v, 0, size);  
  11.     } else {  
  12.         // The array representing the String is the same  
  13.         // size as the String, so no point in making a copy.  
  14.         v = originalValue;  
  15.     }  
  16.     this.offset = 0;  
  17.     this.count = size;  
  18.     this.value = v;  
  19.     }  

这个示例也给我们一个启示,当写一个函数传递数组时,不要直接对内部成员赋值,否则结果就不可控了, 比如下面这个函数,如果myArray被某个成员函数改变了,那么传递的这个数组也会改变。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void setArray(String[] newArray)  
  2.     {  
  3.         this.m_myArray = newArray;  
  4.     }   

而应该这样实现比较好,

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void setArrayNew(String[] newArray)  
  2.     {  
  3.         if(newArray == null)  
  4.         {  
  5.             this.m_myArray = new String[0];  
  6.         }  
  7.         else  
  8.         {  
  9.             this.m_myArray = new String[newArray.length];  
  10.             System.arraycopy(newArray, 0this.m_myArray, 0, newArray.length);  
  11.         }  

  1.     }  

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值