Java - Ternary Operator, inline if (iif), or ternary if
Java offers the ternary operator ? : and it also reffered as conditional operator, inline if (iif), or ternary if. It allows the inline conditional execution exactly similar like if...else...
statement but the if...else... statemnt is a procedureal statement but terniary operator ? : is an inline function and it return a value of same data type.
Example: #1 - inline if (iif), or ternary if Statement
Check if the two numbers are equal then print the message from the success block otherwise print the message from the failure block (i.e. else block).
Java Input Screen
class ConditionalStatement {
public static void main(String[] args) {
int a = 3;
int b = 6;
String result = (a == b) ? "Both the numbers are equal" : "Both the numbers are NOT equal";
System.out.println(result);
}
}
Java Output Screen
Both the numbers are NOT equal
Example: #2 - inline if (iif), or ternary if with compiler error
Check if the value of a is 5
then assign the value 6.3 of type double
otherwise assign the value 9 of type int to the variable
result of type int.
It will throw a compile error while on compile process because the it will expect the data type of both block should be same.
Java Input Screen
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
*
* Both the data type of x and y should be same but the x is double and y is int
* so it requires a proper casting otherwise it will give throw the Type mismatch error
*/
int result = a == 5 ? x : y;
}
}
Java Output Screen: Compiler Error
Type mismatch: cannot convert from double to int
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3 - inline if (iif), or ternary if Statement with no left hand operand
Check if the two numbers are equal then print the message from the success block otherwise print the message from the failure block (i.e. else block),
but here the assignment variable is missing in the left hand side so it will throw a compile error.
Java Input Screen
class ConditionalStatement {
public static void main(String[] args) {
int a = 3;
int b = 6;
/**
* Compiler Error: The left-hand side of an assignment must be a variable
*
* terniary if requires an operand in the left hand side to receive the value
* otherwise it will throw an compiler error
*/
(a == b) ? "Both the numbers are equal" : "Both the numbers are NOT equal";
}
}
Java Output Screen: Compiler Error
The left-hand side of an assignment must be a variable
Thousands Lived without Love, but not without water. So SAVE WATER.
Java Nested Terniary Operator ? :
The nested Terniary Operator ? : statement in java provides the conditional execution inside another Terniary Operator ? : statement.
Example: #4 - Nested Terniary Operator Statement
Find the student's category from the mark using nested Terniary Operator ? :
Java Input Screen
class ConditionalStatement {
public static void main(String[] args) {
int mark = 60;
String message = mark <= 50
? mark < 30 ? "Below average" : "Average"
: mark < 75
? mark < 60 ? "Performer" : "Good Performer"
: "Out Standing";
System.out.println(message);
}
}
BBMINFO