Java SE笔记: 条件运算符

条件运算符也被称为三元运算符、三目运算符。

该运算符有3个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。

1
variable x = (expression) ? value if true : value if false

实例:

1
2
3
4
5
6
7
8
9
10
public class Test {
public static void main(String args[]) {
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}

以上实例编译运行结果如下:
1
2
Value of b is : 30
Value of b is : 20

文章目录