我有一些这样的代码:
StringBuilder rgba = new StringBuilder();
for (...){
....
rgba.append(value + ", ");
}
IntelliJ
向我显示以下警告:
string concatenation as argument to 'stringbuilder.append()' call
来自here , here和其他来源,我知道串联实际上创建了一个新的 String 对象,并且否定了使用 StringBuilder
的全部意义。
所以 IntelliJ
建议我使用
rgba.append(value);
rgba.append(", ");
而不是
rgba.append(value + ", ");
但这真的会更好吗?
这段代码会不会更清晰一些?
这会使执行速度更快吗?
最佳答案
根据 IntelliJ 推理:
Reports String concatenation used as the argument to StringBuffer.append(), StringBuilder.append() or Appendable.append(). Such calls may profitably be turned into chained append calls on the existing StringBuffer/Builder/Appendable, saving the cost of an extra StringBuffer/Builder allocation. This inspection ignores compile time evaluated String concatenations, which when converted to chained append calls would only worsen performance.
所以,我想说:加油!
关于java - StringBuilder.append() 是否应该始终优先于串联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66529296/