Java Ternary Operator

You can use these shortcuts for better readability when you have lots of if-else statements

A = B == C 
This is read as:
If B = C then A is true, else A is false
The statement above is the same as this if-else statement:
if(B == C){
A = true;
} else {
A = false;
}
The other one has a more familiar form than the previous:
A = A ? B : C
This is read as:
If A is true it will take the value of B, If A is false else A will be C.
The statement above is the same as this if-else statement:
if(A == true){
A = B;
} else {
A = C;
}
Note: A,B, and C should return a boolean value.


About this entry


0 comments: