What is the output of the following code
class Main { //from w w w. ja va2 s .c o m public static void main(String args[]) { int a = 1; int b = a ? 2:3; System.out.println(b); } }
Type mismatch: cannot convert from int to boolean
In Ternary, three-way, ? Operator expression:
expression1 ? expression2 : expression3
expression1
must evaluate to a boolean value.
We can fix it by:
class Main { // www . ja v a 2 s.c om public static void main(String args[]) { int a = 1; int b = a==1 ? 2:3; System.out.println(b); } }