System.out.println("1 + 2 = " + 1 + 2);
输出:12
你能解释一下这是为什么吗?我尝试查看一些文档,但没有找到任何内容...
最佳答案
由于 +
运算符是从左到右工作的,因此它首先将字符串 "1 + 2 = "
添加到 1
中,并得到“1 + 2 = 1”
,然后添加 2
得到 “1 + 2 = 12”
。
相当于
System.out.println(("1 + 2 = " + 1) + 2);
试试这个。
System.out.println("1 + 2 = " + (1 + 2));
关于java - System.out.println 的输出给出了奇怪的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22619992/