Currently I’m using:
int a=10;
if(a=20)
printf("TRUE");
else
printf("false");
Which prints, in C, the value TRUE.
But in case of java:
int a=10;
if(a=20)
System.out.println("TRUE");
else
System.out.println("FALSE");
I’ll get a compile time error about an incompatible type.
The reason for that is that in C there is no specific type
boolean– instead any non-0 integer evaluates to a boolean “true”. Thus in your C code:ais assigned the value 20, which is non-0 – and the condition is evaluated astrueIn java, there’s a fundamental type
booleanand the value of the conditional insideifmust be of this type.in Java assigns 20 to
aand returns the final result of evaluation asinteger value 20, however typebooleanis expected – hence you’re getting a compile-time error about the incompatible types.If you want to do a comparison of
awith 20, however, you need to use==operator both in C and Java:This will compile in both C and Java and print FALSE in both languages.