Topic: StringBuffer 真的比String 好?

  Print this page

1.StringBuffer 真的比String 好? Copy to clipboard
Posted by: Jakie
Posted on: 2005-07-06 12:13

我看到文章上说:
String sample1=new String(“Builder.com”);
sample1+=”is”;
sample1+=”the place”;
sample1+=”to be.”;
创建了四个对象
StringBuffer sb=new StringBuffer();
sb.append(“B”);
sb.append(“u”);
sb.append(“i”);
sb.append(“l”);
sb.append(“d”);
sb.append(“e”);
sb.append(“r”);
sb.append(“.com”);
System.out.println(sb.toString());
创建了一个对象
--------是这样吗?第一个例子我感觉也是创建了四个对象,不过第二个例子里面用了好多""这样的引号,在java里面这样的元素不是自动被转换成string了吗例如"a"就是一个String对象,这样说来stringBuffer 真的能提高效率?

2.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: why
Posted on: 2005-07-06 20:17

不信自誤

要比較的話,該用:
StringBuffer sb=new StringBuffer(“Builder.com”);
sb.append(“is”);
sb.append(“the place”);
sb.append(“to be.”);

3.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-07-06 21:26

String and StringBuffer are created for different purposes. In your case, StringBuffer is more efficient!

4.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: Jakie
Posted on: 2005-07-06 21:48

什么啊 什么回复阿!

5.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: why
Posted on: 2005-07-06 22:19

Jakie wrote:
什么啊 什么回复阿!

什么啊 什么問題阿!
什么啊 什么反應阿!

坦白說,敝人也看(猜)不懂閣下想問甚麼.
要怪,請怪自己沒有認真寫清楚問題.

>第一个例子我感觉也是创建了四个对象,
String is not mutable, so each sample1+= 创建一个String对象

>不过第二个例子里面用了好多""这样的引号,在java里面这样的元素不是自动被转换成string了吗例如"a"就是一个String对象,
They are String literals and don't need to be 创建 at runtime.

>这样说来stringBuffer 真的能提高效率?
QuestionQuestionQuestion

What I wanted to say is that the comparison is not meaningful if you use (apple)
StringBuffer sb=new StringBuffer();
sb.append(“B”);
sb.append(“u”);
sb.append(“i”);
sb.append(“l”);
sb.append(“d”);
sb.append(“e”);
sb.append(“r”);
sb.append(“.com”);
to compare with (orange)
String sample1=new String(“Builder.com”);

You should use (orange')
StringBuffer sb=new StringBuffer(“Builder.com”);
instead.

6.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: Jakie
Posted on: 2005-07-07 16:54

"They are String literals and don't need to be 创建 at runtime."
我英文不好,能详细解释吗? 特别是“String literals ”。请全文用简体中文回复,要不就别回复了,回复我也看不懂。

7.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: 4ngle
Posted on: 2005-07-07 21:13

我看到文章上说:
String sample1=new String(“Builder.com”);
sample1+=”is”;
sample1+=”the place”;
sample1+=”to be.”;
创建了四个对象//这也不一定建了四个对象,要看情况.就跟String a= new String("dd");一个道理

8.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: Jakie
Posted on: 2005-07-07 21:25

谢谢~看来那篇文章的作者 水平不怎么样,我刚刚看了一篇关于String 和StringBuffer 的详细解释 看了java 汇编之后 明白了不少。

9.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: why
Posted on: 2005-07-07 21:27

Jakie wrote:
"They are String literals and don't need to be 创建 at runtime."
我英文不好,能详细解释吗? 特别是“String literals ”。请全文用简体中文回复,要不就别回复了,回复我也看不懂。

要全文用简体中文详细解释?
那敝人不回覆了--事实上又回覆了

繁体字显示有问题吗?请问看不见,看不懂还是不看繁体字?

String literals 中文是啥呢?字面值?
String s = "abc";
int i = 123;
abc 是 string literal, 123 是 numeric/integer literal

10.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: Jakie
Posted on: 2005-07-08 14:33

谢谢你的回复,我有一点不明白 是“and don't need to be 创建 at runtime."”
的深层含义 能解释一下吗? 谢谢。

11.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: javadd
Posted on: 2005-07-08 16:00

String 好看,
StringBuffer 好用.
我个人理解

12.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: why
Posted on: 2005-07-08 23:21

Jakie wrote:
谢谢你的回复,我有一点不明白 是“and don't need to be 创建 at runtime."”
的深层含义 能解释一下吗? 谢谢。

String literals are stored in a "Literal Pool" and don't need to be constructed (创建); on the other hand, a String instance, constructed at runtime, is stored in the heap.
Don't ask me to explain in Chinese, I simply don't know (am not interested in, is puzzled by the variations of, could not remember) the proper translation.

13.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: Jakie
Posted on: 2005-07-08 23:26

感谢回复 ,不好意思。我刚刚复制完你说的话,然后去www.readworld.com翻译一下

14.Re:StringBuffer 真的比String 好? [Re: Jakie] Copy to clipboard
Posted by: justin_here
Posted on: 2005-07-13 08:13

public class AppendStringTest {
public static void main(String[] args) {
String text = "";

long beginTime = System.currentTimeMillis();
for(int i = 0; i < 10000; i++)
text = text + i;
long endTime = System.currentTimeMillis();
System.out.println("執行時間:" + (endTime - beginTime));

StringBuffer buffer = new StringBuffer("");
beginTime = System.currentTimeMillis();
for(int i = 0; i < 10000; i++)
buffer.appendLight Bulb;
endTime = System.currentTimeMillis();
System.out.println("執行時間:" + (endTime - beginTime));
}
}


執行時間:4484
執行時間:16


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923