/** * Initializes a newly created <code>String</code> 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. * * @param original a <code>String</code>. */ public String(String original) { this.count = original.count; if (original.value.length > this.count) { // 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. this.value = new char[this.count]; System.arraycopy(original.value, original.offset, this.value, 0, this.count); } else { // The array representing the String is the same // size as the String, so no point in making a copy. this.value = original.value; } }
这是java.lang.String里说的(version j2sdk 1.4.1_01)
原文
* <p><blockquote><pre>
* String str = "abc";
* </pre></blockquote><p>
* is equivalent to:
* <p><blockquote><pre>
* char data[] = {'a', 'b', 'c'};
* String str = new String(data);
* </pre></blockquote><p>
第一种写法说到底只有“AVC”一个String,s只是一个引用而已
第二种写法说到底也只有"ave"一个String,S也只是一个引用
不知道你们说的第二种比第一种多创建一个对象在哪呢?
String s="avc"; //建立一个String对象,将值"avc"付给它
String s=new String("avc"); //建立一个String对象,再建立建立一个String对象将值付给第二个对象,然后将第二个对象的引用付给第一个对象,,,,,,,,
第二种方法多产生了一个对象,多了一次赋值
* Initializes a newly created <code>String</code> 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.
*
* @param original a <code>String</code>.
*/
public String(String original) {
this.count = original.count;
if (original.value.length > this.count) {
// 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.
this.value = new char[this.count];
System.arraycopy(original.value, original.offset,
this.value, 0, this.count);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
this.value = original.value;
}
}