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

The part I'm stuck with all the time is boolean(0 % 2 !=0) == false. I mean, if 2 goes to 0, 0 times, then the remainder will be 2, and 2 will not be 0. Therefore, this should be true. However, but when I add a boolean to my java program, it treats it as false. Does anyone know why?

The only logical answer I can turn my head to is that maybe the integers go to 0 and infinitely many times and therefore are considered false, anyone?

+6
source share
6 answers

There are two steps:

  • 0 % 2 evaluates to 0 .

  • 0 != 0 evaluates to false .

To elaborate on the first step, JLS defines the % operator as follows:

The binary operator% is said to return the remainder of its operands from implied division; the left operand is a dividend, and the right operand is a divisor.

The rest of dividing 0 by 2 is 0 , not 2 , as you think.

+24
source

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

You just misunderstood the module.

+4
source

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

Therefore, 0 % 2 is 0 .

+2
source

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

OK, let me ...

1) 0 % 2

modulo is the rest of the final division. For example, 10% 3 is the rest of 10/3. 10/3 is 3 + β…“. So the rest is 1.

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

2) 0 % 2 != 0

This means that 0% 2 is different from 0. Now we know that this is not true.

3) boolean(0 % 2 != 0)

It is just casting. You bring the result to Boolean. Instead of just being a false assumption, it gets the Java value false .

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

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

+1
source

if 2 goes 0, 0 times, then the remainder will be 2.

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

0
source

This is due to the priority of the operator, that is, the order in which operators are evaluated by the Java interpreter.

See here for documents. One useful acronym is BUDMASRELCA - B , Unary, D iv- M Multiplication (actually multiplicative includes modulo), A ddittion- S ubtraction, R , E , L strong> ogical, onditional (triple), And ssignment. I skipped the bitwise operators, but they can be grouped according to logic, and they take precedence over normal logical operators.

0% 2! = 0 is first evaluated as 0% 2 (multiplicative), and then the result 0 is evaluated using! = 0. (equality)

Internally, compilers build a binary expression tree to represent the order as shown below for your case, using operators as roots and leaving them as values ​​or further operators (in the recursive case). Thus, subtrees that have statements must be evaluated before the root statement can be evaluated with the value that it leaves.

  != / \ % 0 /\ 0 2 
0
source

Source: https://habr.com/ru/post/915259/


All Articles