Ternary operator in Java at the end of the return statement

Viewed 29

I want to understand why the following code does not work.

private int calculate(int param) {
   int a = 0; int b = 0;
   return a + b + param == 0 ? 1 : 0;
}

I know you can make the function above work like this

private int calculate(int param) {
   int a = 0; int b = 0;
   int val = param == 0 ? 1 : 0;
   return a + b + val;
}

but I want to understand why the first code does not work.

Thanks in advance!

0 Answers
Related