1.1
logical Operators
The logical operator, which is used for Boolean operations, and the final result of the operation is a Boolean value of True or false.
Operator |
Arithmetic rules |
Example |
Results |
& |
And |
False&true |
False |
| |
Or |
False|true |
True |
^ |
XOR or |
True^flase |
True |
! |
Non - |
!true |
Flase |
&& |
Short Circuit and |
False&&true |
False |
|| |
Short Circuit or |
false| | True |
True |
After reading the graph, let's look at the general use of logical operators:
L logical operators usually concatenate two other expressions to evaluate the result of a Boolean value
L when the use of short-circuit and or short-circuit or, as long as the results can be judged, the back part is no longer judged.
int x = 1,y = 1;
if (x++==2 & ++y==2)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);
& and, go through the conditions, regardless of the outcome of the right and wrong. The loop body is entered when the condition is satisfied.
int x = 1,y = 1;
if (x++==2 && ++y==2)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);
&& Short circuit and, a short circuit will not go, if the first result is false, then the back will not go, the direct end (jump out of the loop), if not go down.
int x = 1,y = 1;
if (x++==1 | ++y==1)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);
| or, as long as there is a condition to be satisfied, enter the loop body, and go through the whole condition
int x = 1,y = 1;
if (x++==1 | | ++y==1)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);
|| Short circuit or, as long as there is a condition to not go behind, such as the first meet the conditions, do not go behind, directly into the loop body.
Java Logical operator Understanding