简体   繁体   English

Java算术:为什么它们不是“ 9 = 9”?

[英]Java arithmetic: why aren't they “9 = 9”?

I have a question on java arithmetic with integers and strings. 我对整数和字符串的Java算术有疑问。 For example, 例如,

int a = 1;
int b = 3;
int c = 5;
System.out.println(a + b + (c + " = ") + a + (b + c));
System.out.println((a + b) + c + " = " + a + b + c);
System.out.println(a + (b + c) + " = " + (a + b) + c);

The code above outputs "45 = 18", "9 = 135", and "9 = 45" respectively. 上面的代码分别输出“ 45 = 18”,“ 9 = 135”和“ 9 = 45”。 I don't understand the logic behind this operation. 我不了解此操作背后的逻辑。 My first instinct was they all output "9 = 9". 我的第一个直觉是它们都输出“ 9 = 9”。 I would like somebody to help me understand this operation. 我希望有人可以帮助我了解此操作。 I appreciate your help! 我感谢您的帮助!

Addition is left-associative but parenthesis can change the order of execution. 加法是左关联的,但是括号可以更改执行顺序。

So if we have to break down the first println here, when we write a+b , it results in arithmetic addition (5) , but when we do c + " = " + a + b + c , it results in string concatenation 5=9 , because c + " = " evaluates first and make the expression as String + int operation resulting in string concatenation. 因此,如果我们必须在这里分解第一个println ,那么当我们编写a+b ,会导致算术加法(5) ,但是当我们执行c + " = " + a + b + c ,就会导致字符串连接5=9 ,因为c + " = "首先求值,并将表达式设为String + int运算,导致字符串串联。 Remember, int+int is int and String+int is String 记住, int+intintString+intString

Because of parenthesis () , how the expression evaluates changes. 由于括号() ,表达式的求值方式发生了变化。 This is how the above expression evaluates if we include parenthesis 如果我们包含括号,这就是上面表达式的计算方式

(c + " = ") + a + (b + c)
 - First it evaluates (c + " = "), so the expression becomes 5 = + a + (b + c)
 - Now it evaluates b+c because of parenthesis, , so the expression becomes 5 = + a + 8
 - Now as there are not parenthesis, it evaluates the expression from left to 
   right and as the first operand is string, the whole expression becomes a
   string concatenation operation

Complete breakdown of first expression 完全分解第一个表达式

a + b + (c + " = ") + a + (b + c)
- First precedence is of (b + c), so now it becomes a + b + (c + " = ") + a+8
- Next precedence  is of (c + " = "), so now it becomes a + b + "5 = " + a+8
- Now as there is not (), expression is evaluated from left to right, so
  now it evaluates a + b , so it becomes 4 + "5 = " + a+8
- now it evaluates '4 + "5 = "', so it becomes `45 = a + 8`, because we have 
  now a string in the expression, so it does string concatenation
- and it becomes `45 = 18`

Similarly you can breakdown the other two expression 同样,您可以分解其他两个表达式

The point here is that you are mixing int addition + with the string concatenation + operation. 这里的要点是您正在将int加法+与字符串串联+操作混合在一起。

In the you compute 1+3, resulting in 4. Then you put that in front of a string that is "5 = 1" which is followed by the result of 5 + 3 (8). 在中,您计算​​1 + 3,得出4。然后将其放在字符串“ 5 = 1”前面,然后是5 + 3(8)。

The different results are then based on the different effects placing the braces. 然后基于放置括号的不同效果得出不同的结果。

If you concat int with string then it will result in string, and by adding brackets it changes the execution structure Your example: System.out.println(a + b + (c + " = ") + a + (b + c)); 如果用字符串连接int,则将生成字符串,并通过添加括号将执行结构更改为您的示例:System.out.println(a + b +(c +“ =”)+ a +(b + c) );

  1. (b + c) will resolve to 8 (b + c)将解析为8
  2. (c + " = ") will be resolved to "5=" (c + " = ")将解析为“ 5 =”
  3. a + b which is at start of statement will result in 4 语句开始处的a + b将导致4
  4. a + b + (c + " = ") this statement will become a string value 4+"5=" and output will be "45=" a + b + (c + " = ")该语句将成为字符串值4+“ 5 =”,输出将为“ 45 =”
  5. then this will get concatenated with a + b + (c + " = ") + a and result in "45=" + 1 and results in "45=1" 那么它将与a + b + (c + " = ") + a串联,结果为“ 45 =” + 1,结果为“ 45 = 1”
  6. so the whole statement will become a + b + (c + " = ") + a + (b + c) resolved to "45=1" + 8 so the final result is "45=18" 因此整个语句将变为a + b + (c + " = ") + a + (b + c)解析为“ 45 = 1” + 8,因此最终结果为“ 45 = 18”

There are two meanings for + operation. +操作有两个含义。 First meaning is arithmetic addition operation between multiple numbers value. 第一个含义是多个数值之间的算术加法运算。 For example 例如

System.out.println(1 + 2 + 5);  

Above print result is 9. 上面的打印结果是9。
Second meaning is String concatenation between String and others. 第二个含义是String与其他字符串之间的String串联。 For example 例如

System.out.println(9 + "=" + 9);  

Above print result is "9=9". 上面的打印结果是“ 9 = 9”。
I think you may want to print the commutative law of addition. 我认为您可能希望打印加法的交换定律。 Following may be what you want. 以下可能是您想要的。

int a = 1, b = 3, c = 5;
System.out.println( (( a + b ) + c ) + "=" + ( a + ( b + c )) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM