Logic & and short-circuit && in Java, logic | and Short Circuit | | The difference

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.