版本是java8
基本类型和对象
java的string是什么呢? 很明显是对象
特化的+
15.18.1. String Concatenation Operator + If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time. 来源
java的字符串连接符是+
,而php的是.
stringbuilder
string常量折叠
由于上面提到的jls8中的String Concatenation Operator +
提到相关内容,如果操作符中只有一个string类型的话,类型转换会发生在运行时.没有规定两个都是string的时候怎么处理,所以javac将他折叠了
/** If tree is a concatenation of string literals, replace it
* by a single literal representing the concatenated string.
*/
protected JCExpression foldStrings(JCExpression tree) {
if (!allowStringFolding)
return tree;
ListBuffer<JCExpression> opStack = new ListBuffer<>();
ListBuffer<JCLiteral> litBuf = new ListBuffer<>();
boolean needsFolding = false;
JCExpression curr = tree;
while (true) {
if (curr.hasTag(JCTree.Tag.PLUS)) {
JCBinary op = (JCBinary)curr;
needsFolding |= foldIfNeeded(op.rhs, litBuf, opStack, false);
curr = op.lhs;
} else {
needsFolding |= foldIfNeeded(curr, litBuf, opStack, true);
break; //last one!
}
}
if (needsFolding) {
List<JCExpression> ops = opStack.toList();
JCExpression res = ops.head;
for (JCExpression op : ops.tail) {
res = F.at(op.getStartPosition()).Binary(optag(TokenKind.PLUS), res, op);
storeEnd(res, getEndPos(op));
}
return res;
} else {
return tree;
}