How java converts int to boolean

When I convert:

int B=1;
boolean A=B;

It gives an error: Incompatible types that are true

But when I write this code:

int C=0;
boolean A=C==1;

it gives false while if I change the value of C to 1, it gives true. I do not understand how the compiler does it.

+4
source share
3 answers
int C=0;
boolean A=C==1;

The compiler first gives C zero.

Variable : C
Value    : 0

Now the assignment operator

We know that the assignment operator first evaluates the right side and gives it to the left.

Right part ==> C == 1 Here is an expression that evaluates to trueor false. In this case, this is not true asc is 0.

Therefore, RHS is false.

Now it is assigned to LHS, which is A.

A = ( C == 1 ) ==> A = false

Since it Ais logical, this is a valid statement.

+3

C==1 - , boolean ( ). true, C 1 false .

boolean A=C==1; boolean boolean.

+3

First he checks c==1and the result is assigned A.

how Cis not equal to 1, so the expression evaluates to false, which is assignedA

+2
source

All Articles