Java 算术加法和字符串连接之间的“+”运算符?
Java '+' operator between Arithmetic Add & String concatenation?
据我们所知
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
这是基本的运算符优先级,结合 String
连接与数字加法。
引用:
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.
The result of string concatenation is a reference to a String object
that is the concatenation of the two operand strings. The characters
of the left-hand operand precede the characters of the right-hand
operand in the newly created string.
The String object is newly created (§12.5) unless the expression is a
constant expression (§15.28).
An implementation may choose to perform conversion and concatenation
in one step to avoid creating and then discarding an intermediate
String object. To increase the performance of repeated string
concatenation, a Java compiler may use the StringBuffer class or a
similar technique to reduce the number of intermediate String objects
that are created by evaluation of an expression.
For primitive types, an implementation may also optimize away the
creation of a wrapper object by converting directly from a primitive
type to a string.
请参阅语言规范 here。
TL;DR
+
的运算符优先级从左到右
- 如果二元运算中的任何操作数是
String
,则结果是 String
- 如果两个操作数都是数字,则结果是一个数字
+
的计算顺序是从左到右,它是这样工作的:
System.out.println("3" + 3 + 3);
当您尝试这样做时,由于第一个参数是 String
,其余所有内容都将是字符串连接
System.out.println(3 + "3" + 3);
这个也一样,因为你不能把第一个 int 3
添加到 String 3
然后最后一个 3
将连接到第二个 3
System.out.println(3 + 3 + "3");
首先从左到右计算表达式(给出 3+3 = 6),然后将字符串 3
附加到该计算结果中,给出 63
作为输出
这与字符串连接本身无关。它是关于隐式转换为 String 的。它从左到右运行,在上述情况下没有隐式括号。并且 String 优先于 int.
示例 1
System.out.println("3" + 3 + 3);
在这里,它以字符串开头,因此在执行 + 运算符之前,此后的每个操作都隐式转换为字符串。因此,“333”。
示例 2
System.out.println(3 + "3" + 3);
此处同样适用,因为字符串优先于初始 3。
示例 3
System.out.println(3 + 3 + "3");
在这种情况下,前两个数字相加,因为它们是整数,结果是 6,但是当它接下来被添加到字符串时,它假定连接,因此是“63”。
根据 specification.
据我们所知
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
这是基本的运算符优先级,结合 String
连接与数字加法。
引用:
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.
The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.
请参阅语言规范 here。
TL;DR
+
的运算符优先级从左到右- 如果二元运算中的任何操作数是
String
,则结果是String
- 如果两个操作数都是数字,则结果是一个数字
+
的计算顺序是从左到右,它是这样工作的:
System.out.println("3" + 3 + 3);
当您尝试这样做时,由于第一个参数是String
,其余所有内容都将是字符串连接System.out.println(3 + "3" + 3);
这个也一样,因为你不能把第一个
int 3
添加到String 3
然后最后一个3
将连接到第二个3
System.out.println(3 + 3 + "3");
首先从左到右计算表达式(给出 3+3 = 6),然后将字符串
3
附加到该计算结果中,给出63
作为输出
这与字符串连接本身无关。它是关于隐式转换为 String 的。它从左到右运行,在上述情况下没有隐式括号。并且 String 优先于 int.
示例 1
System.out.println("3" + 3 + 3);
在这里,它以字符串开头,因此在执行 + 运算符之前,此后的每个操作都隐式转换为字符串。因此,“333”。
示例 2
System.out.println(3 + "3" + 3);
此处同样适用,因为字符串优先于初始 3。
示例 3
System.out.println(3 + 3 + "3");
在这种情况下,前两个数字相加,因为它们是整数,结果是 6,但是当它接下来被添加到字符串时,它假定连接,因此是“63”。
根据 specification.