假设字符串a和b:
a += b
a = a.concat(b)
Run Code Online (Sandbox Code Playgroud)
引擎盖下,它们是一样的吗?
这里以concat反编译为参考.我希望能够反编译+运算符以查看它的作用.
public String concat(String s) {
int i = s.length();
if (i == 0) {
return this;
}
else {
char ac[] = new char[count + i];
getChars(0, count, ac, 0);
s.getChars(0, i, ac, count);
return new String(0, count + i, ac);
}
}
Run Code Online (Sandbox Code Playgroud) 我们知道
Java'+'运算符用于两者
当我一起使用时,需要知道确切预期的行为和应用规则
当我尝试遵循java代码
System.out.println("3" + 3 + 3); // print 333 String concatenation ONLY
System.out.println(3 + "3" + 3); // print 333 String concatenation OLNY
System.out.println(3 + 3 + "3"); // print 63 Arithmetic Add & String concatenation
Run Code Online (Sandbox Code Playgroud)