The meaning and usage of ?: (ternary operator) in Java

The meaning and usage of ? in Java

? : In fact, it is a ternary operator in Java, which means a conditional judgment statement, and judges a Boolean statement, that is, A ? B : C, which means: if statement A is true, execute statement B, and if statement A is false , execute statement C.

The code example is as follows:

n == 1 ? "The value of n is equal to 1" : "The value of n is not equal to 1";

time >= 23 ? "Sleep" : "Eat";

What the ternary operator does:

Simplify code, improve code readability,

As a null check, an example is as follows:

return result != null ? 1 : 0;

Returns 1 if result is not null, otherwise returns 0.

Guess you like

Origin blog.csdn.net/qq_27246521/article/details/129710824