StringBuilder.append() 应该总是比串联更受欢迎吗?

Should StringBuilder.append() be always preferred than concatenation?

我有一些这样的代码:

StringBuilder rgba = new StringBuilder();  
for (...){
....
rgba.append(value + ", ");
}

IntelliJ 显示以下警告:

string concatenation as argument to 'stringbuilder.append()' call

, here 和其他来源我了解到串联实际上是创建一个新的 String 对象并且否定了使用 StringBuilder.
的全部意义 所以 IntelliJ 建议我使用

rgba.append(value);
rgba.append(", ");  

而不是

rgba.append(value + ", ");  

但这真的会更好吗?
这段代码会不会更清晰?
这会使执行速度更快吗?

在我看来,连接对于开发人员来说更具可读性,但 StringBuilder.append 是高效的

你说的是正确的,因为串联和 StringBuilder 的混合在理论上违背了使用它的观点,但我们正在谈论的额外性能非常小,除非你're 循环有大量的迭代。

就代码清晰度而言,我认为双 append 实际上更清晰,考虑到您仍然在使用它。

我会将这两个语句视为

Append ", " appended on "value"
Append "value"
Append ", "

其中第二个实际上看起来更简洁,即使代码在技术上更长。

根据 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.

所以,我会说:加油!