Example: #2 - inline if (iif), or ternary if with compiler error
Choose 6.3 when a equals 5 and 9 otherwise. Numeric promotion determines the result type.
class ConditionalStatement {
public static void main(String[] args) {
int a = 5;
double x = 6.3;
int y = 9;
/**
* Compiler Error: Type mismatch: cannot convert from double to int
*
* The conditional expression has type double. Assign it to a double
* or explicitly convert it to int if truncation is intended.
*/
int result = a == 5 ? x : y;
}
}
Compile-time error: possible lossy conversion from double to int.