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

家住魔仙堡 提交于 2019-12-19 17:42:37

问题


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. So it should be true. Yet but when I put the boolean in my java program it is treating it as false. Anyone know why?

The only logical answer I can wrap my head around is that maybe integers go into 0 and infinite number of times and so are recognized as false, anyone?


回答1:


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.




回答2:


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

you just misunderstood modulus.




回答3:


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

Therefore 0 % 2 is 0.




回答4:


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.




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/10511170/java-0-2-0-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!