Home > Back-end >  Why cant assign c the expression a > b?
Why cant assign c the expression a > b?

Time:07-23

int a = 2, b = 3; int c = a > b; // found boolean, required int

How can i solve without making c a boolean?

CodePudding user response:

you can not cast int to boolean or boolean to int

you can use ternary statement

int c = (b > a) ? someIntegerValue : anotherIntegerValue;

check table 5-5-A https://docs.oracle.com/javase/specs/jls/se17/html/jls-5.html#jls-5.5

notice any not allowed conversion is forbidding https://docs.oracle.com/javase/specs/jls/se17/html/jls-5.html#jls-5.1.12

for more details check this https://docs.oracle.com/javase/specs/jls/se17/html/jls-5.html#jls-5.1

hope that help and have a nice day :)

CodePudding user response:

Is it a good solution to do int c = bool(b > a)?

  • Related