,&& and &,| in Java | The difference from |

Source: Internet
Author: User

There are four classes in the logical operator (in Java):

&& (short circuit and),&

|| (short circuit or), |

Short circuit and is a logical operator in the Java language, which is recorded as &&amp, and is similar to logic in programming languages, but has short-circuit properties. The symbol is:&&. A&&b, when a is false, returns false without calculating the value of B, and evaluates the value of B when a is true. (from Baidu Encyclopedia) and A&b, you need to calculate the values of a and B to return a value. In simple terms:&& and & are both expressed and, the difference is && if the first condition is not satisfied, the latter condition is no longer judged. & has to judge all the conditions. Example 1
 Public class demo{publicstaticvoid  main (string[] args) {        int i = 4;         if ((i++ > 6) & (i++ < 9)) {            System.out.println (i);            System.out.println ("If is true");        }        System.out.println (i);    }}

Operation Result: 6

Since I have added two times.

Example 2:

 Public class demo{publicstaticvoid  main (string[] args) {        int i = 4;         if ((i++ > 6) && (i++ < 9)) {            System.out.println (i);            System.out.println ("If is true");        }        System.out.println (i);    }}

Operation Result: 5

Since I was added once.

Similarly, | and | | is a similar situation.

Baidu Encyclopedia:

A short-circuit or a logical operator in the Java language, written as | |. The so-called short circuit, that is, when the first item is true, the second judgment is no longer made.

a| | B: Only A and B are false, the result is false, there is a true, and the result is true.

In simple terms:

      || and | Both are expressed with, the difference is | | If the first condition is not satisfied, the latter condition is no longer judged. and | You have to judge all the conditions.

,&& and &,| in Java | The difference from |

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.