Why no error on dividing by zero

Namaste everyone,

Why does “int x = 200/0;”
gives an error but

int x = 200/20*0; gives the value as 0.?

Thanks.

Because 200/20*0 is (200/20)*0, not 200/(20*0).

4 Likes

Cheers
— mnse

3 Likes

Hello @prathmesh,

A search for:

operator precedence Java

will reveal the answer to your question.

The Oracle Java documentation:

Parentheses discussed here:

When writing compound expressions, be explicit and indicate with parentheses which operators should be evaluated first. This practice makes code easier to read and to maintain.

:)

3 Likes

Thanks for the links :smiley:

1 Like