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

后端 未结 6 996
闹比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: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.

提交回复
热议问题