我刚开始在高中学习 AP Comp sci,我偶然发现了一个关于字符串中 + 运算符的问题
为什么
System.out.println ("数字"+ 6 + 4 * 5)
结果为 number620
鉴于
String s = "crunch";
int a = 3, b = 1;
System.out.print(s + a + b);
System.out.print(b + a + s);
crunch314crunch
的结果?
谢谢
最佳答案
When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition (+).
关于java - + 运算符和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18757312/