Short-circuit AND is a logical operator in the JAVA language, recorded as &&
A&&B. When A is false, the value of B is not calculated and false is returned directly; when A is true, the calculation The value of B.
Example:
public static void main(String[] args){
int a=10, b=20;
System.out.println("original a="+a );
System.out.println("original b="+b);
boolean bool;
if(a++==11 && b++==21) {
System.out.println ("true");
System.out.println("a="+a);
System.out.println("b="+b);
} else {
System .out.println("false");
System.out.println("a="+a);
System.out.println("b="+b);
}
}
Output result:
Original a=10
Original b=20
false
a=11
b=20 (note here, b’s The value has not changed)
Non-short-circuit AND is a logical operator in the JAVA language, recorded as &
A&&B. When A is false, continue to calculate B value without directly returning false (compared with short-circuit AND?)
Example:
public static void main(String[] args){
int a=10, b= 20;
System.out.println("original a="+a);
System.out.println("original b="+b);
boolean bool;
if(a++ ==11 & b++==21) {
System.out.println("true");
System.out.println("a="+a);
System.out.println( "b="+b);
} else {
System.out.println("false");
System.out.println("a="+a);
System. out.println("b="+b);
}
}
Output result:
Original a=10
Original b=20
false
a=11
b=21 (note here, the value of b has changed)
The difference between short-circuit OR and non-short-circuit OR can be verified by yourself! ! !
The above is the detailed content of The difference between short circuit and && (or ||) and non-short circuit and & (or |). For more information, please follow other related articles on the PHP Chinese website!