Basic
Short-circuit operators are commonly used "&" and "|", which are generally called "Conditional operations ".
Class logic {
Public ststic void main (string [] ARGs ){
Int A = 1;
Int B = 1;
If (A <B & B <A/0 ){
System. Out. println ("Oh, that's impossible !!! ");
} Else {
System. Out. println ("that's in my control .");
}
}
}
The & operator checks whether the first expression returns "false". If it is "false", the result must be "false" and no other content is checked.
"A/0" is an obvious error! However, when the short-circuit operation "&" first judges "A <B" and returns "false", it causes a short circuit and does not perform the "A/0" operation, the program will output "that's in my control. ". In this case, the program immediately throws an exception "Java. Lang. arithmeticexception:/by zero" in the exchange of expressions on both sides ".
class Logic{
public ststic void main(String[] args){
int a=1;
int b=1;
if(a==b || b<a/0){
System.out.println("That's in my control.");
}else{
System.out.println("Oh,That's Impossible!!!");
}
}
}
"|" Operator checks whether the first expression returns "true". If it is "true", the result must be "true" and no other content is checked.
"A/0" is an obvious error! However, the short-circuit computation "|" first executes "A = B" and returns "true", which causes a short-circuit and does not perform the "A/0" operation, the program will output "that's in my control. ". In this case, the program immediately throws an exception "Java. Lang. arithmeticexception:/by zero" in the exchange of expressions between the left and right sides ".
Non-short-circuit operators include "&", "|", "^", and are generally called "logical operations"
class Logic{
public ststic void main(String[] args){
int a=1;
int b=1;
if(a<b & b<a/0){
System.out.println("Oh,That's Impossible!!!");
}else{
System.out.println("That's in my control.");
}
}
}
The "&" operator will not cause short circuits. It will seriously check every expression. Although "A <B" has returned "flase", it will continue to check other content, so that an exception "Java. lang. arithmeticexception:/by zero ".
/By zero ".
class Logic{
public ststic void main(String[] args){
int a=1;
int b=1;
if(a==b | b<a/0){
System.out.println("That's in my control.");
}else{
System.out.println("Oh,That's Impossible!!!");
}
}
}
Similarly, the "|" operator does not cause short circuits. Although "A = B" has returned "true", it will continue to check other content, so that an exception "Java. lang. arithmeticexception:/by zero ".
The "^" operator is the same, so it is not here.
Last. Short-circuit operators can only be used in logical expressions. Non-short-circuit operators can be used in BIT expressions and logical expressions. It can also be said that the short-circuit operation can only operate on the boolean type, rather than the short-circuit operation can operate not only on the boolean type, but also on the numeric type.