Java之把String对象作为方法的参数

来源:互联网 发布:windows最新系统 编辑:程序博客网 时间:2024/06/02 02:38

参考自:http://blog.csdn.net/pony_maggie/article/details/44120045


看一段代码:

public class StringTest {public void StringPassTest(String s, String[] ss) {s = "abc";ss[0] = "abc";}public static void main(String[] args) {String s1 = "good";String[] ss = { "edf" };StringTest stringTest = new StringTest();stringTest.StringPassTest(s1, ss);System.out.println(s1 + ss[0]);}}

输出结果:


从输出结果与可以看出:s1的值竟然没改!ss的值改了。

对于函数StringPassTest来说,String 类型的传递是引用传递,也即是地址传递。这个是毋庸置疑的。因为在java里,String是对象类型,作为参数肯定是引用传递。


ss参数完全符合引用传递的特点,很好理解。


下面来看一下String的构造函数实现,版本为jdk1.8:

    /**     * Initializes a newly created {@code String} object so that it represents     * the same sequence of characters as the argument; in other words, the     * newly created string is a copy of the argument string. Unless an     * explicit copy of {@code original} is needed, use of this constructor is     * unnecessary since Strings are immutable.     *     * @param  original     *         A {@code String}     */    public String(String original) {        this.value = original.value;        this.hash = original.hash;    }

this.value为:

 /** The value is used for character storage. */    private final char value[];

所以从以上代码可以看出:String内部是用char[]来存储字符串的,String相当于char[]的包装类,在Java中,包装类的特质就是值操作时体现对应的基本类型的特质,这就是为什么会有传值的效果!


这个示例也给我们一个启示:当写一个函数传递数组的时候,不要直接对内部成员进行赋值,否则结果就是不可控的。

比如下面这个函数,如果m_myArray被改了,那么传递的这个数组也会改变,因为是引用传递!


public void setArray(String[] newArray)      {          this.m_myArray = newArray;      } 


最好使用下面的方法:


public void setArrayNew(String[] newArray)      {          if(newArray == null)          {              this.m_myArray = new String[0];          }          else          {              this.m_myArray = new String[newArray.length];              System.arraycopy(newArray, 0, this.m_myArray, 0, newArray.length);          }      } 




0 0
原创粉丝点击