Source: http://www.imooc.com/code/1301
Logical operators are primarily used for logical operations. The logical operators commonly used in Java are shown in the following table:
We can understand logical operators from the perspective of "voting":
1, and : Ask everyone to vote for consent, to pass an issue
2, or : Just ask a person to vote for permission to pass an issue
3, non : Someone originally voted to agree, through the non-operator, you can make their votes invalid
4. XOR : There is only one person who can vote to approve the issue.
When using logical operators, we encounter a very interesting " short circuit " phenomenon.
For example: (One > both) && (one < three), if you can determine that the left one > The run result is false, then the system thinks there is no need to execute the one < three on the right.
Similarly, in (One > both) | | (One < three), you can determine that the left expression will run as true, then the system also thinks there is no need to do the one < three on the right.
Task
Assuming that a, B, C, D four people vote, they decide whether to vote, it is time for the logical operator "to". please fill in the code in the editor 7, 8, 9, line, add the program complete
Combined operation Result:
, I believe that can help you better understand the application of logical operators ~ ~
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 BooleanA =true;//a agree4 Booleanb =false;//b against5 Booleanc =false;//C Objection6 BooleanD =true;//D Agree7 8 9 Ten One A } -}
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 BooleanA =true;//a agree4 Booleanb =false;//b against5 Booleanc =false;//C Objection6 BooleanD =true;//D Agree7System.out.println ((a && B) + "failed");8System.out.println ((a | | d) + "pass");9System.out.println ((!a) + "failed");TenSystem.out.println ((c ^ d) + "pass"); One } A}
Web-android Engineer first form logical operators in -3-6 Java