The operation rule of print is that before encountering the first character/string/variable, all numbers will be added when encountering the plus sign. When any character is encountered, the plus sign becomes a character/string concatenation. Is that so?
The operation rule of print
討論中
留言 (1)
- 受歡迎
- 新
- 舊
你必須登入才能留言
Thomas
11 十二月 2022, 10:15
as you said, the + sign may have two meanings, addition and concatenation.
if one of the two operands is a String, then the compiler tries to convert the other operand to a String, too and then concatenates the results, like 1 + "test" -> "1test"
if one operand is a char and the other an int, then the compiler will chose an addition as a char is basically a short, so '1' + 1 -> 50
if one operand is an object an the other a String, then the compiler calls implicitly the toString() method on the object and concatenates the result.
Evaluation starts on the left. If you eg. have the term: 1 + 1 + "worlds", then the compiler first looks at 1 + 1 and that's 2, then it's looking at 2 + "worlds" and here it'll chose the concatenation and the final result is "2worlds"
So the final answer is yes and no. The compiler will chose addition for numbers and chars and when it finds a String it'll concatenate.
0