Short form for Java if statement
In Java, you can use the ternary operator (also known as the conditional operator) to create a short form for an if-else statement.
Here's the syntax for using the ternary operator:
condition ? expression1 : expression2
If the condition
is true
, the expression evaluates to expression1
. If the condition
is false
, the expression evaluates to expression2
.
Here's an example of how you can use the ternary operator to create a short form for an if-else statement:
int a = 10;
int b = 20;
int min = (a < b) ? a : b;
This code is equivalent to the following if-else statement:
int a = 10;
int b = 20;
int min;
if (a < b) {
min = a;
} else {
min = b;
}