下了个反编译工具看了下却实是哈 源码这是: package com.string;public class test { public static void main(String[] args) { String s = "a"+"b"+"c"+"d"+"e"; } } 这是反编译后的代码// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 2010-8-15 上午 10:56:49 // Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version! // Decompiler options: packimports(3) // Source File Name: test.javapackage com.string; public class test { public test() { } public static void main(String args[]) { String s = "abcde"; } }
下了个反编译工具看了下却实是哈 源码这是: package com.string;public class test { public static void main(String[] args) { String s = "a"+"b"+"c"+"d"+"e"; } } 这是反编译后的代码// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 2010-8-15 上午 10:56:49 // Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version! // Decompiler options: packimports(3) // Source File Name: test.javapackage com.string; public class test { public test() { } public static void main(String args[]) { String s = "abcde"; } } 支持啊!!要动手
8个吧 String s = "a" + "b" + "c" + "d" + "e"; 相当于 String s = new String("a") + new String("b") + new String("c") + new String("d") + new String("e");
就创建了一个 String s = "a" + "b" + "c" + "d" + "e"; 赋值符号右边的"a"、"b"、"c"、"d"、"e"都是常量 对于常量,编译时就直接存储它们的字面值而不是它们的引用 在编译时就直接讲它们连接的结果提取出来变成了"abcde" 该语句在class文件中就相当于String s = "abcde" 然后当JVM执行到这一句的时候, 就在String pool里找 如果没有这个字符串,就会产生一个
但是如果改成 String s = a+b+c+d+e; 呢 又是几个了。 就是说上面是一个是因为 "a"、"b"、"c"、"d"、"e"都是常量 但如果是变量呢? 我的答案是3个对象,但只有一个String对象:由于编译器的优化,最终代码为通过StringBuilder完成: StringBuilder builder = new StringBuilder(); builder.append(a); builder.append(b); builder.append(c); builder.append(d); builder.append(e); String s = builder.toString(); 我们先看看StringBuilder的构造器 public StringBuilder() { super(16); } 看下去 AbstractStringBuilder(int capacity) { value = new char[capacity]; } 可见,分配了一个16自己长度的char数组我们看看append的整个过程(注意,源代码我从各个类进行了整合,他们实际上不在一个类里面的) public StringBuilder append(String str) { super.append(str); return this; } public AbstractStringBuilder append(String str) { if (str == null) str = "null"; int len = str.length(); if (len == 0) return this; int newCount = count + len; if (newCount > value.length) expandCapacity(newCount); str.getChars(0, len, value, count); count = newCount; return this; } public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { if (srcBegin < 0) { throw new StringIndexOutOfBoundsException(srcBegin); } if (srcEnd > count) { throw new StringIndexOutOfBoundsException(srcEnd); } if (srcBegin > srcEnd) { throw new StringIndexOutOfBoundsException(srcEnd - srcBegin); } System .arraycopy(value, offset + srcBegin, dst, dstBegin, srcEnd - srcBegin); } 可见,我们的代码不会超过16个,所以不会出现扩展value的情况。 而append里面使用了arraycopy的复制方式,也没有产生新的对象。最后,我们再看StringBuilder的 toString()方法: public String toString() { // Create a copy, don't share the array return new String(value, 0, count); } 这里通过前面的数组生成了一个新的String。大家注意那个默认的16容量,如果题目出现了总长度超过16,则会出现如下的再次分配的情况 void expandCapacity(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; } else if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } value = Arrays.copyOf(value, newCapacity); } public static char[] copyOf(char[] original, int newLength) { char[] copy = new char[newLength]; System .arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; } 可见,expand容量时,增加为当前(长度+1)*2。 注意这里用了Arrays的方法,注意不是前面的 System.arraycopy方法哦。这里产生了一个新的 copy的char数组,长度为新的长度 总结:三个对象分别为 1 StringBuilder 2 new char[capacity] 3 new String(value,0,count);如果说String对象,则为1个。
只有一个对象。String s
按照顺序 = 号最后 前面都是每个String 相加啊哎!!
坐等高手解答
源码这是:
package com.string;public class test {
public static void main(String[] args) {
String s = "a"+"b"+"c"+"d"+"e";
}
}
这是反编译后的代码// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 2010-8-15 上午 10:56:49
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: test.javapackage com.string;
public class test
{ public test()
{
} public static void main(String args[])
{
String s = "abcde";
}
}
源码这是:
package com.string;public class test {
public static void main(String[] args) {
String s = "a"+"b"+"c"+"d"+"e";
}
}
这是反编译后的代码// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 2010-8-15 上午 10:56:49
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: test.javapackage com.string;
public class test
{ public test()
{
} public static void main(String args[])
{
String s = "abcde";
}
}
支持啊!!要动手
String s = "a" + "b" + "c" + "d" + "e";
相当于
String s = new String("a") + new String("b") + new String("c") + new String("d") + new String("e");
String s = "a" + "b" + "c" + "d" + "e";
赋值符号右边的"a"、"b"、"c"、"d"、"e"都是常量
对于常量,编译时就直接存储它们的字面值而不是它们的引用
在编译时就直接讲它们连接的结果提取出来变成了"abcde"
该语句在class文件中就相当于String s = "abcde"
然后当JVM执行到这一句的时候, 就在String pool里找
如果没有这个字符串,就会产生一个
呢 又是几个了。 就是说上面是一个是因为 "a"、"b"、"c"、"d"、"e"都是常量
但如果是变量呢? 我的答案是3个对象,但只有一个String对象:由于编译器的优化,最终代码为通过StringBuilder完成:
StringBuilder builder = new StringBuilder();
builder.append(a);
builder.append(b);
builder.append(c);
builder.append(d);
builder.append(e);
String s = builder.toString();
我们先看看StringBuilder的构造器
public StringBuilder() {
super(16);
}
看下去
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
可见,分配了一个16自己长度的char数组我们看看append的整个过程(注意,源代码我从各个类进行了整合,他们实际上不在一个类里面的) public StringBuilder append(String str) {
super.append(str);
return this;
} public AbstractStringBuilder append(String str) {
if (str == null)
str = "null";
int len = str.length();
if (len == 0)
return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
} public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > count) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System
.arraycopy(value, offset + srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
可见,我们的代码不会超过16个,所以不会出现扩展value的情况。
而append里面使用了arraycopy的复制方式,也没有产生新的对象。最后,我们再看StringBuilder的 toString()方法:
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
这里通过前面的数组生成了一个新的String。大家注意那个默认的16容量,如果题目出现了总长度超过16,则会出现如下的再次分配的情况
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
} public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System
.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
可见,expand容量时,增加为当前(长度+1)*2。
注意这里用了Arrays的方法,注意不是前面的 System.arraycopy方法哦。这里产生了一个新的
copy的char数组,长度为新的长度
总结:三个对象分别为
1 StringBuilder
2 new char[capacity]
3 new String(value,0,count);如果说String对象,则为1个。
builder.append(b);
builder.append(c);
builder.append(d);
builder.append(e);"
请问你这个a,b,c,d,e变量所指的不是5个String对象?
String a = new String();
a = "a";
那么"a" + "b" + "c" + "d" + "e"; 数一下
String a = "a" //一个
不过我感觉讨论这没什么意义啊
那1.3、1.4是什么原理呢
继续关联下去,n多对象
a,b,c,d,e,ab,abc,abcd,abcde
"a"+"b"=ab一个
"ab"+"c"=abc一个
"abc"+"d"=abcd一个
"abcd"+"e"=abcde一个一共创建了9个对象,String是一个final类;如果这里用StringBuffer那就是创建一个,因为StringBuffer有append方法实现字符的串联。
String s = "abcde";
老师教我的是 应该二个对像啊? 怎么回事,求解答。
只有一个对象,如果是
String a =“a”;
String b = “b”;
.......
a + b 将是产生2个对象,依次类推。
由于Java编译器的优化,这个东西应该在编译器就被优化过了。
等号右侧全是常量,创建了一个对象。