将String +运算符与StringBuilder混合使用

时间:2017-01-22 18:00:52

标签: java string stringbuilder string-concatenation

假设我有以下代码:

String RET = System.getProperty("line.separator");
int x = 5, y = 3;

StringBuilder sb = new StringBuilder();
sb.append("Ship located at " + x + ", " + y + RET);
sb.append("Current time: " + System.nanoTime());
return sb.toString();

编译器是否聪明并且这样做:

StringBuilder sb = new StringBuilder();
sb.append("Ship located at ");
sb.append(x);
sb.append(", ");
sb.append(y);
sb.append(RET);
sb.append("Current time: ");
sb.append(System.nanoTime());
return sb.toString();

或者它会愚蠢并且这样做:

StringBuilder sb = new StringBuilder();
sb.append(new StringBuilder().append("Ship located at ").append(x).append(", ").append(y).append(RET).toString());
sb.append(new StringBuilder().append("Current time: ").append(System.nanoTime()).toString());
return sb.toString();

或者它会做其他事情吗?

0 个答案:

没有答案