1, in Java & called the bitwise AND,&& is called short-circuit with, the difference is:
& both the bitwise operator and the logical operator,& can be either int or a Boolean expression, and when the & sides are int, the number of sides of the operator is converted to a binary number before the operation, and the short-circuit (&& Both sides of the request must be a Boolean expression. Examples are as follows:
What is the value of 12&5? A: 12 turns into a binary number is 1100 (the first four bits omitted), 5 turns into a binary number is 0101, then the result of the operation is 0100 that is 4 when the two sides are numeric values;
if int i = 2,J = 4, then (++i=2) & (j++=4) The result is false, the process is this: first determine whether the ++i=2 is established, here is of course not established (3 = = 2), but the program will continue to determine whether the next expression is set, j++=4, The expression is true, but the & operator requires that the values on both sides of the operator be true, and the result is real, so (++i=2) & (j++=4) results in false note that:& is true if both expressions are true. But even if we judge the value of the left expression to be false, the program will continue to execute to determine the true and false value of the expression on the right.
if int i = 2,J = 4, then (++i=2) && (j++=4) The result is false, the process is basically the same as above, but if the value of the left expression is false, the program will not continue to judge the true and false of the right expression, short-circuit and in, The word "short" is probably what this means.
2. ' | ' in Java With ' | | ' The difference
int i=0;
if (3>2 | | (i++) >1) i=i+1;
System.out.println (i);
This program prints 1 instead of printing out 2.
Because in the conditional judgment of the IF, the program first to determine whether the first expression 3>2, the result is 3>2 true, then according to the logic, regardless of the subsequent expression (i++) >1 whether or not, the whole or expression is definitely true, so the program does not go to judge the following expression is (i + +) >1, so here I did not increase by 1. Then the program executes to i=i+1, so I becomes 1. Finally print out 1.
int i=0;
if (3>2 | (i++) >1) i=i+1;
System.out.println (i);
If this is done, then it is printed out 2, because regardless of whether the first condition 3>2 is true, the program will be executed to judge the second conditional expression, so i++ this self-increment will be executed, plus if the i=i+1, so the final i=2.
Some people are used to putting | | Called Short circuit or, && is called short circuit with. && and & Differences and | | Like the difference between |, for &&, if the previous conditional expression is false, the entire conditional expression is definitely false, and the conditional expression after the judgment is not executed.
Java in &, |, &&, | | Detailed