First, the use of logical operators
1) The logical operator is a Boolean expression that is to be distinguished from the bitwise operator.
2) How to use:
public class Test {
public static void Main (string[] args) {
System.out.println (True & false);//result is False
System.out.println (True & True);//result is true
System.out.println (False & False);//result is False
System.out.println (False & True);//result is False
/*
System.out.println (True && false);//result is False
System.out.println (True && true);//result is true
System.out.println (False && false);//result is False
System.out.println (False && true);//result is False
*/
}
}
Conclusion: Logic is true only if the expression on both sides of the symbol is true
==============================================================
public class Test {
public static void Main (string[] args) {
System.out.println (True | false);//result is true
System.out.println (True | true);//result is true
System.out.println (False | false);//result is False
System.out.println (False | true);//result is true
/*
System.out.println (True | | false);//result is true
System.out.println (True | | true);//result is true
System.out.println (False | | false);//result is False
System.out.println (False | | true);//result is true
*/
}
}
Conclusion: A logical or false result only if the expression on either side of the symbol is false
==============================================================
Second, ' short circuit and ', ' short circuit or '
1) ' &, | ' With ' &&, | | ' The difference
A single logical operator evaluates the left and right two expressions to derive a Boolean value and then perform the operation. ' Short circuit and ' if the left expression is false, the right expression will not be judged,
Because the result must be false, a ' short-circuit or ' if the left-hand expression evaluates to true does not judge the right expression because the result must be true.
The logical operator of a phrase is more efficient than a normal logical operator.
Iii. examples
public class Test {
public static void Main (string[] args) {
int num = 1;
System.out.println (False & Num++==1);//Even if the front is false, the expression on the right will continue to execute, i.e. num++
SYSTEM.OUT.PRINTLN (num);//2
System.out.println ("===== Gorgeous split line =====");
num = 1;
System.out.println (False && num++==1);//The left expression is false and the right expression will not execute, that is, Num will not increment
SYSTEM.OUT.PRINTLN (num);//1
}
}
The result of the input is:
False
2
===== Gorgeous split-line =====
False
1
Short circuit and is the same reason, you can write your own code verification
==============================================================
Short-circuiting (&) in Java with (&&) and OR (|) Short circuit or (| | ) of the relationship