Java conditional operator

https://java-er.com/blog/java-condition-operation/

Java provides an especially ternary operator (also called trinocular operator ) are often substituted for a certain type of if-then-else statements

Variable x = (condition) value if true:? Value if false

example

int c = a > b ? a:b;

Explanation

The assignment to c if a> b then c = a set up if a> b then c = b is not satisfied
int a = 5;
int b = 10;
int c;
c = a > b ? a : b;
System.out.println("c value is " + c);
 
c = a > b ? 1 : 2;
System.out.println("c value is " + c);
c value is 10
c value is 2

Guess you like

Origin www.cnblogs.com/yuexiaosheng/p/12324834.html