那要看情况
"abc"是一个常量字符串
如果这句话String a = new String("abc"); 之前,你已经用过“abc”的话,那么这句话直接引用那个常量,创建一个新的String对象,并把常量“abc”的内容拷贝到新分配的字符串空间
如果没有,那么首先要先创建并分配新的“abc”常量,然后进行相同的动作
"abc"是一个常量字符串
如果这句话String a = new String("abc"); 之前,你已经用过“abc”的话,那么这句话直接引用那个常量,创建一个新的String对象,并把常量“abc”的内容拷贝到新分配的字符串空间
如果没有,那么首先要先创建并分配新的“abc”常量,然后进行相同的动作
你看一下api里对这个构造函数的解释
String(String)
分配一个新 String ,它和字符串参数中有相同的字符序列
String str=new String("csdn");
StringBuffer sb=new StringBuffer();
sb.append(str);这是java类库String类标准运行程序。
http://dev.csdn.net/develop/article/38/38155.shtm
public String(String original)
Initializes a newly created 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 original is needed, use of this constructor is unnecessary since Strings are immutable. Parameters:
original - a String.
接着有String a=new String("abc");
那么执行String a=new String("abc")这条时, 只创建一个对象.
接着有String a=new String("abc");
那么执行String a=new String("abc")这条时, 只创建一个对象.对不对啊?
* 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. Unless
* an explicit copy of <code>original</code> is needed, use of this
* constructor is unnecessary since Strings are immutable.
*
* @param original a <code>String</code>.
*/
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;
}
String a = new String("abc");"abc"
a
String a = new String("abc");
a = new String(...) 一个
"abc" JVM一查共享字串表,还没用过,按虚拟机规范,JVM创建一个"abc"的String,并返回它的堆地址如果"abc"用过,则只创建一个a = new String(...)对像,而"abc"这样的常量字符是共享的,JVM直接返回已创建的“abc”的String实例的堆地址。
程序代码中的每一个"..."这样的常量字符,运行时都对应了一个String实例,这一点大家可以从以下代码中说明:int l = "abc".length()JDK的String.java中也注明了这一点
"abc"就是一个字符数组char类型的常量,java8种基本类型中的一种。
例:String a=String.valueOf(123);
a=a+5;//这时候a的地址和第一次已经不一样了,a已经又是一个新对象了。
共享常量区中是否存在"abc"字符串常量。 fast_time(fast_time) , catblue(limiaomiao), huqingmiao(梦里不知身是蝶),HITZXL(编程要厚道), QQSu35(kaka),binny(骑个破车看夕阳) ,fohoo(飞狐) 是正确的。 YOUTAO89(),( ) sunant(天使之泪) QQSu35(kaka) , catblue(limiaomiao)
sunant(天使之泪),lizhiwo_wo(lizhiwo_wo) 理解错误或没完全理解。