Why System.out.println(3+(4-3)/2); is printing 3 but not 2 in java but System.out.println(4/2) is printing 2 correctly

huangapple go评论125阅读模式
英文:

Why System.out.println(3+(4-3)/2); is printing 3 but not 2 in java but System.out.println(4/2) is printing 2 correctly

问题

为什么 System.out.println(3+(4-3)/2); 在 Java 中输出的是 3 而不是 2,但 System.out.println(4/2) 正确地输出为 2。

public class Sum {
    public static void main(String[] args) {
        System.out.println(3 + (4 - 3) / 2);
    }
}
英文:

Why System.out.println(3+(4-3)/2); is printing 3 but not 2 in java but System.out.println(4/2) is printing 2 correctly.

    public class Sum {

        public static void main(String[] args) {
	        System.out.println(3 + (4 - 3) / 2);
        }
    }

答案1

得分: 2

就像在数学中一样,在Java中,乘法和除法的优先级高于加法和减法。因此,您的表达式等同于以下内容:

        3 + ((4 - 3) / 2)

所以让我们一步一步来:

        3 + (4 - 3) / 2 = 3 + ((4 - 3) / 2)
                        = 3 + (1 / 2)
                        = 3 + 0
                        = 3

正如其他人所说,Java中的整数除法与数学中的除法不同,因为任何余数都被丢弃。在学校里,我们学到1除以2等于商0余1。在Java中,我们只得到0。

顺便说一下:要获得余数1,可以执行1 % 2(4 - 3) % 2。如果我们希望除法的结果是0.5,我们需要除以浮点数值,例如1.0 / 2.0(4.0 - 3.0) / 2.0

历史背景:我在这里应用的计算规则绝不仅适用于Java。精确的规则是从C++和C中采用的,这些规则可能来自更早的编程语言。在几乎所有编程语言中,您都会找到非常相似的规则。

您是否期望从左到右进行评估,括号除外?另一对括号可以确保这一点:

		System.out.println((3 + (4 - 3)) / 2);

输出为:

> 2

我从您的问题中理解到,这是您期望的输出。

随着嵌套括号越来越多,阅读起来变得困难,所以最好先将值分配给变量:

		int result = (3 + (4 - 3)) / 2;
		System.out.println(result);

输出当然还是一样的。您可能已经意识到,计算现在进行如下:

        (3 + (4 - 3)) / 2 = (3 + 1) / 2
                          = 4 / 2
                          = 2

链接: Java中的运算符优先级

英文:

Just as in math also in Java multiplication and division have higher precedence than addition and subtraction. Therefore your expression is equivalent to the following:

    3 + ((4 - 3) / 2)

So let’s just take it step by step:

    3 + (4 - 3) / 2 = 3 + ((4 - 3) / 2)
                    = 3 + (1 / 2)
                    = 3 + 0
                    = 3

As others have said, division of integers in Java is unlike division in math since any remainder is discarded. In school we learned that 1 / 2 is 0 with a remainder of 1. In Java we just get the 0.

As an aside: To have the remainder, the 1, you may do 1 % 2 or (4 - 3) % 2. And if we want the result of the division to be 0.5, we need to divide floating point values, for example 1.0 / 2.0 or (4.0 - 3.0) / 2.0.

Historic perspective: The rules for calculation that I have applied here are by no means unique to Java. The precise rules have been taken over from C++ and C, which may have taken them from even older programming languages. And in practically all programming languages you will find pretty similar rules.

Had you expected left to right evaluation except for the brackets? Another pair of brackets can ensure that:

	System.out.println((3 + (4 - 3)) / 2);

Output is:

> 2

I understood from your question that this was the output you had expected.

It’s becoming hard to read with this many nested brackets, though, so prefer to assign the value to a variable first:

	int result = (3 + (4 - 3)) / 2;
	System.out.println(result);

Output is still the same, of course. You have probably already realized that the calculation now goes:

    (3 + (4 - 3)) / 2 = (3 + 1) / 2
                      = 4 / 2
                      = 2

Link: Operator Precedence in Java

huangapple
  • 本文由 发表于 2020年7月24日 03:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63062107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定