In Java's logical operators, there are four categories: && (short-circuit and), &, |, || (short-circuit or).
Both && and & represent Yu, the difference is that && as long as the first condition is satisfied, the latter condition is no longer judged. And & to judge all the conditions.
See the program below:
public static void main(String[] args) { // TODO Auto-generated method stub if((23!=23)&&(100/0==0)){ System.out.println("There is no problem with the operation."); } }
The output is "no problem with the operation", and no error is reported. And changing && to & will give the following error:
Exception in thread "main" java.lang.ArithmeticException: / by zero
The reason is: when && judges the first condition as false, the following condition 100/0==0 is not judged.
When &, all conditions must be judged, so the following conditions will be judged, so an error will be reported.
||Both and | represent "or", the difference is that || As long as the first condition is satisfied, the following conditions are no longer judged, and | needs to judge all conditions.
See the program below:
public static void main(String[] args) { // TODO Auto-generated method stub if((23==23)||(100/0==0)){ System.out.println("There is no problem with the operation."); } }
At this time, "the operation is OK" is output. If you change || to |, an error will be reported.
The reason is: || judges the first condition to be true, and executes the code in parentheses without judging the following conditions, and | needs to judge all conditions,
so an error will be reported.