还是老问题:String创建对象的数量

踏雁寻花 2014-03-21 01:31:32
String result = foo() + arg;
result += boo();
System.out.println(“result = “ + result);

创建了几个StringBuilder对象。
引用网上的一个回复:
In this example we have 3 StringBuilders allocated in the background – one for each plus operation, and two additional Strings – one to hold the result of the second assignment and another to hold the string passed into the print method. That’s 5 additional objects in what would otherwise appear to be a pretty trivial statement.
这个回答是否有道理。

另外:
StringBuilder value = new StringBuilder(“result = “);
value.append(foo()).append(arg).append(boo());
System.out.println(value);

又会有几个StringBuilder对象?
还是引用:
allocating only one StringBuilder and one String to hold the final result, instead of the original five objects.
看下是否有道理?
...全文
256 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxbccsu 2014-03-24
  • 打赏
  • 举报
回复
引用 7 楼 tayanxunhua 的回复:
[quote=引用 4 楼 lxbccsu 的回复:] [quote=引用 楼主 tayanxunhua 的回复:]
String result = foo() + arg;
result += boo();
System.out.println(“result = “ + result);
创建了几个StringBuilder对象。 引用网上的一个回复: In this example we have 3 StringBuilders allocated in the background – one for each plus operation, and two additional Strings – one to hold the result of the second assignment and another to hold the string passed into the print method. That’s 5 additional objects in what would otherwise appear to be a pretty trivial statement. 这个回答是否有道理。 另外:
StringBuilder value = new StringBuilder(“result = “);
value.append(foo()).append(arg).append(boo());
System.out.println(value);
又会有几个StringBuilder对象? 还是引用: allocating only one StringBuilder and one String to hold the final result, instead of the original five objects. 看下是否有道理?
上面说的都是对的; 首先Java中是不可以重载操作符,但这个 + 能用于字符串操作,也就是重载了操作符,这也是Java中唯一操作符重载的地方; 其次,任何字符串的 + 操作都会转换为StringBuilder的操作; 但你若这样操作: StringBuilder value = new StringBuilder(“result = “ + foo()); value.append(arg + boo()); 那你又是白优化了,还是会创建3个 StringBuilder对象; [/quote] "allocating only one StringBuilder and one String to hold the final result,"这里的"one String to hold the final result"指的是哪个?[/quote] 这里说的是StringBuilder 的toString方法,这个方法会把StringBuilder的内容转变为String,当你调用System.out.println(value)时,就会把这个最终的结果打印出来;
king_1993 2014-03-24
  • 打赏
  • 举报
回复
好深奥的样子
代码间的舞者 2014-03-22
  • 打赏
  • 举报
回复
学习了,标记下
踏雁寻花 2014-03-21
  • 打赏
  • 举报
回复
引用 4 楼 lxbccsu 的回复:
[quote=引用 楼主 tayanxunhua 的回复:]
String result = foo() + arg;
result += boo();
System.out.println(“result = “ + result);
创建了几个StringBuilder对象。 引用网上的一个回复: In this example we have 3 StringBuilders allocated in the background – one for each plus operation, and two additional Strings – one to hold the result of the second assignment and another to hold the string passed into the print method. That’s 5 additional objects in what would otherwise appear to be a pretty trivial statement. 这个回答是否有道理。 另外:
StringBuilder value = new StringBuilder(“result = “);
value.append(foo()).append(arg).append(boo());
System.out.println(value);
又会有几个StringBuilder对象? 还是引用: allocating only one StringBuilder and one String to hold the final result, instead of the original five objects. 看下是否有道理?
上面说的都是对的; 首先Java中是不可以重载操作符,但这个 + 能用于字符串操作,也就是重载了操作符,这也是Java中唯一操作符重载的地方; 其次,任何字符串的 + 操作都会转换为StringBuilder的操作; 但你若这样操作: StringBuilder value = new StringBuilder(“result = “ + foo()); value.append(arg + boo()); 那你又是白优化了,还是会创建3个 StringBuilder对象; [/quote] "allocating only one StringBuilder and one String to hold the final result,"这里的"one String to hold the final result"指的是哪个?
zhjdg 2014-03-21
  • 打赏
  • 举报
回复
我对 + += 不了解,所以第一个不知道。 第2个对 one String to hold the final result,我不同意。 System.out.println(value); value是StringBuilder 看源代码一清二楚。
-江沐风- 2014-03-21
  • 打赏
  • 举报
回复
lxbccsu 2014-03-21
  • 打赏
  • 举报
回复
引用 楼主 tayanxunhua 的回复:
String result = foo() + arg;
result += boo();
System.out.println(“result = “ + result);
创建了几个StringBuilder对象。 引用网上的一个回复: In this example we have 3 StringBuilders allocated in the background – one for each plus operation, and two additional Strings – one to hold the result of the second assignment and another to hold the string passed into the print method. That’s 5 additional objects in what would otherwise appear to be a pretty trivial statement. 这个回答是否有道理。 另外:
StringBuilder value = new StringBuilder(“result = “);
value.append(foo()).append(arg).append(boo());
System.out.println(value);
又会有几个StringBuilder对象? 还是引用: allocating only one StringBuilder and one String to hold the final result, instead of the original five objects. 看下是否有道理?
上面说的都是对的; 首先Java中是不可以重载操作符,但这个 + 能用于字符串操作,也就是重载了操作符,这也是Java中唯一操作符重载的地方; 其次,任何字符串的 + 操作都会转换为StringBuilder的操作; 但你若这样操作: StringBuilder value = new StringBuilder(“result = “ + foo()); value.append(arg + boo()); 那你又是白优化了,还是会创建3个 StringBuilder对象;
-江沐风- 2014-03-21
  • 打赏
  • 举报
回复
StringBuffer和StringBuilder基本上差不多,就是线程的问题; String定义的是常量,即String实例化以后所有的属性都是final的,而StringBuffer和StringBuilder确不是,这就是所谓的可变与不可变; 一个比喻:String是一个商品; StringBuffer/StringBuilder是生产这个商品的流水线; StringBuffer速度慢,但(线程)安全性高; StringBuilder速度快,但(线程)安全性差;
  • 打赏
  • 举报
回复
使用javac -p 看看,这个问题没必要纠结了

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧