为什么字符串连接与pip行在java中没有返回任何内容(空字符串)?

itr*_*tro 0 java string concatenation

为什么字符串连接与pip行在java中没有返回任何内容(空字符串)?

String product="";
for(Tar t:tars){
    product.concat(t.product.concat("|"));
}
System.out.println(product);
Run Code Online (Sandbox Code Playgroud)

结果就是什么(空字符串).

nul*_*ent 9

String#concat返回一个连接的String,它不会修改它.字符串在Java中是不可变的.

所以...

product = product.concat(t.product.concat("|"));
Run Code Online (Sandbox Code Playgroud)

但是,我建议使用StringBuilder,其中String复制在循环中发生.