1. With (&,&&) and OR (|,| |) The difference
1.1 When using and operating, it is necessary to ask the contents of several expressions to be true, the end result is true, and if one is false, the end result is false;
1.2 When using or operating, ask the front and back several expressions as long as there is a true, the end result is true, if all is false, then the final result is false;
2. Differences with (&) and Short circuits (&&) and/or (|) and short circuit or (| |) The difference
This is the beginner, the confused people will be more
First say and operation: Since the operation requires the content of several expressions before and after the final result is true, if the use of short-circuit and, as long as the first result is false, the overall result is definitely false, but the program at this time will not continue to execute, on the contrary, if not short-circuit and , even if the first result is false, the program will continue to perform other results, although the end result is the same, give an example, clear:
Java code
- public class test1{
- public Static void main (string args[]) {
- if (10!=10&10/0==0) {
- system.out.println ( "condition satisfies") ;
- }
- }
- };
Apparently, 10! =10 The result is false, but because there is no use of short-circuit with, the program will continue to execute the second condition result judgment, the second in the program obviously exception, so this code compiled can pass, but the operation will be error!
Look at the code again:
public class test2{
public static void Main (String args[]) {
if (10!=10&&10/0==0) {
SYSTEM.OUT.PRINTLN ("Condition meets");
}
}
};
The first result is false, the program will not be executed, so the program is normal operation;
Next Talk or (|) and short circuit or (| |) The difference
In fact, you understand the difference between the two before, this is very simple
Or it means that the expression as long as there is a true, the result is true, all expressions are false, the result is false;
Short-circuit and representation as long as the first expression is true, the program does not perform other expression judgments, and conversely, if it is not a short-circuit or, even if the first one is true, all other expressions are executed.
Java code
- Public class test3{
- public static void Main (String args[]) {
- if (10==10| 10/0==0) {
- System.out.println ("condition meets");
- }
- }
- };
Obviously, this program execution will error, because the program will judge each expression, and the following:
Java code
- Public class test4{
- public static void Main (String args[]) {
- if (10==10| | 10/0==0) {
- System.out.println ("condition meets");
- }
- }
- };
The above program because the first expression is true, and the use of a short-circuit or, the program will no longer perform the following judgment, all programs run normally;
With (&,&&) and OR (|,| |) The difference