`java (0 % 2 != 0) == false`

后端 未结 6 987
闹比i
闹比i 2021-01-18 04:55

The part I keep getting stuck on is boolean(0 % 2 !=0) == false. I mean if 2 goes into 0, 0 times then the remainder would be 2, and 2 does not equal 0. S

相关标签:
6条回答
  • 2021-01-18 05:16

    It has to do with Operator Precedence, that is the order with which operators are evaluated by the Java interpretor.

    See here for the docs. One useful acronym is BUDMASRELCA - Brackets, Unary, Div-Multiplication (actually multiplicative as it includes modulo), Addittion-Subtraction,Relational,Equality,Logical,Conditional(ternary),Assignment. I've missed out Bitwise operators, but they could be grouped under Logical, and they take precedence over normal logical operators.

    0 % 2 !=0 is evaluated as 0%2 (multiplicative) first and then it's result 0 is evaluated with != 0. (equality)

    Internally, compilers build a binary expression tree to represent the order as shown below for your case, using operators as roots and leaves as values or further operators (in the recursive case). So the sub-trees that have operators need to be evaluated before the root operator can be evaluated with the value of it's leaves.

      !=
     / \
    %   0
    /\
    0  2
    
    0 讨论(0)
  • 2021-01-18 05:21

    0%2 = 0, because 0/2 = 0 and reminder 0, or 0*2+reminder = 0.

    you just misunderstood modulus.

    0 讨论(0)
  • 2021-01-18 05:22

    There are two steps:

    • 0 % 2 evaluates to 0.

    • 0 != 0 evaluates to false.

    To elaborate on the first step, the JLS defines the % operator like so:

    The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

    The remainder of dividing 0 by 2 is 0 and not 2 as you seem to think.

    0 讨论(0)
  • 2021-01-18 05:23

    % returns the remainder after the division. Zero divided by anything (except itself!) is zero, so there is no remainder.

    Therefore 0 % 2 is 0.

    0 讨论(0)
  • 2021-01-18 05:38

    I think you mix up 0%2 and 2%0 (which is impossible). 0%n is always equal to 0.

    Ok, let's dissect that…

    1) 0 % 2

    modulo is the rest of the finite division. For example 10%3 is the rest of 10/3. 10/3 is 3+⅓. So the rest is 1.

    0%2 is the rest of 0/2. 0/2=0, there is no rest, thus 0%2=0.

    2) 0 % 2 != 0

    It means 0%2 is different than 0. We now know it's false.

    3) boolean(0 % 2 != 0)

    It's simply casting. You cast the result to a Boolean. Instead of just being some false assumption, it gets the Java value false.

    4) boolean(0 % 2 != 0) == false

    The == means that there is a test here. The test can be simplified (as shown above) as false == false. Is false equal to false? Yes it is. The result is then true.

    0 讨论(0)
  • 2021-01-18 05:38

    if 2 goes into 0, 0 times then the remainder would be 2.

    it is not 2 goes into 0 but 0 goes into 2, so the result of the devision is 0 and the reminder is 0.

    0 讨论(0)
提交回复
热议问题