Concept
1. The conditional operator is also called the ternary operator. This operator has three operands and needs to determine the value of a Boolean expression.
2. This operator mainly determines which value should be given to the variable.
Grammar form
布尔表达式 ? 表达式1 :表达式2
Operation process
If the value of the Boolean expression is true, the expression is returned The value of expression 1, otherwise the value of expression 2 is returned.
public static void main(String[] args) { int a, b; a = 10; // 如果 a 等于 1 成立,则设置 b 为 20,否则为 30 b = (a == 1) ? 20 : 30; System.out.println("Value of b is : " + b); // 如果 a 等于 10 成立,则设置 b 为 20,否则为 30 b = (a == 10) ? 20 : 30; System.out.println("Value of b is : " + b); }
The above is the detailed content of How to use java conditional operators. For more information, please follow other related articles on the PHP Chinese website!