ternary operator java syntax code example

Example 1: java ternary operator

Object myObject = booleanExpression ? valueIfTrue : valueIfFalse;

Example 2: ternary operator in java

// variable= condition ? value if condition is True : value if condition is false
// only ternary operator in java
int max,a=1,b=2;
max= a>b ? a : b;

//will result in max = b

Example 3: ternärer operator java

int num = 8;
String msg = "";
if(num > 10) {
    msg = "Number is greater than 10";
}
else {
    msg = "Number is less than or equal to 10";
}

Tags:

C Example