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.
看下是否有道理?