The difference between logic & short-circuit && in Java is that logic & needs to judge the two conditions on the left and right of the & symbol, and the short-circuit && If the && symbol is true on the left, it will be judged right; if & The & symbol left is false, then go else.
eg.
Public classDemo { Public Static voidmain (String [] args) {intA = 4; intb = 3; intc = 0; if(A<b & b/c = = 0) {//ErrorSystem.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao"); } if(A>b & b/c = = 0) {//ErrorSystem.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao"); } if(a>b && b/c = = 0) {//ErrorSystem.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao"); } if(a<b && b/c = = 0) {System.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao");//output "Wohenhao" } } }
The above code, because the first, the second paragraph output inside, because it is a logical &, so the & symbol left and right conditions to judge, one of which is false, is not set. And where b/c, because the divisor is 0, so the output times wrong. The third, fourth paragraph output, because it is short-circuit &&, so long as the left of the && is false, will go else,&& the left of the symbol is true, will judge the second condition. So the third paragraph output error, the fourth paragraph output else in the printing results.
eg.
Public classDemo { Public Static voidmain (String [] args) {intA = 4; intb = 3; intc = 0; if(A<b & b/c = = 0) {//ErrorSystem.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao"); } if(A>b | b/c = = 0) {//ErrorSystem.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao"); } if(A>b | | b/c = = 0) {System.out.println ("Nihaoma");//output "Nihaoma"}Else{System.out.println ("Wohenhao"); } if(A<b | | b/c = = 0) {//Error System.out.println ("Nihaoma"); }Else{System.out.println ("Wohenhao"); } } }
In the above code, the first second output, because it is a logical |, so both sides have to judge, there is an error, that is, an error. The third fourth output, because it is a short-circuit | |, so the third paragraph, the first condition is true, will not continue to judge the second condition, the fourth paragraph, because the first is false, need to judge the second, because the second error, so the result of an error.
Logic & Short && in Java, logic | and Short Circuit | | The difference