Java算术:为什么不';他们不是吗;9=9“吗;?

Java算术:为什么不';他们不是吗;9=9“吗;?,java,Java,我有一个关于带整数和字符串的java算法的问题。比如说, 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); 上述代码分别输出“45=18”、“9=135”和

我有一个关于带整数和字符串的java算法的问题。比如说,

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);

上述代码分别输出“45=18”、“9=135”和“9=45”。我不明白这次行动背后的逻辑。我的第一反应是他们都输出“9=9”。我希望有人能帮助我理解这次行动。我感谢你的帮助

加法是左关联的,但括号可以更改执行顺序

因此,如果我们必须在这里分解第一个
println
,当我们写
a+b
时,它会导致算术加法
(5)
,但是当我们做
c+“=”+a+b+c
,它会导致字符串串联
5=9
,因为
c+“=”
首先求值,并将表达式设为
String+int
操作,从而产生字符串连接。记住,
int+int
int
String+int
String

由于括号
()
,表达式的求值方式会发生变化。如果包含括号,上述表达式的计算结果就是这样的

(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
第一个表达式的完全分解

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`

类似地,您可以分解另外两个表达式

这里的要点是将int addition+与string concatenation+操作混合在一起

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`
在这个例子中,你计算1+3,得到4。然后你把它放在一个字符串前面,这个字符串是“5=1”,后面是5+3(8)的结果


不同的结果基于放置支撑的不同效果

如果将int与string合并,则会生成string,并通过添加括号更改执行结构 你的例子是: 系统输出打印项次(a+b+(c+“=”)+a+(b+c))

  • (b+c)
    将解析为8
  • (c+“=”)
    将解析为“5=”
  • 语句开头的a+b将导致4
  • a+b+(c+“=”)
    此语句将成为字符串值4+“5=”,输出为“45=”
  • 然后这将与
    a+b+(c+“=”)
    +a连接,结果为“45=“+1”,结果为“45=1”
  • 因此,整个语句将变成
    a+b+(c+“=”)+a+(b+c)
    解析为“45=1”+8,因此最终结果为“45=18”

  • +操作有两种含义。第一个意思是多个数字之间的算术加法运算。比如说

    System.out.println(1 + 2 + 5);  
    
    以上打印结果为9。
    第二种含义是字符串和其他字符串之间的字符串连接。比如说

    System.out.println(9 + "=" + 9);  
    
    以上打印结果为“9=9”。
    我想你可能想要打印加法的交换定律。下面可能是你想要的

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

    加法是左联想的。括号改变了执行的顺序。int+int是int。int+String或String+int是String(串联)。