Explain the difference between & and && in Java _java

Source: Internet
Author: User

&& (short circuit and), &, | | (short circuit or), |, these operators are often used in Java program development, the following small series is mainly to introduce the difference between & and &&.

where && and & both represent logic and, when the result of the expression on both sides of the operator is true, the operation structure is true.

However, && has a short-circuit effect, and if the first expression is false, you do not have to judge the second expression.

Copy Code code as follows:

if ((str!=null) && (!str.equals ("")) {

SYSTEM.OUT.PRINTLN ("correct operation");
}

If you change && to & you throw a NullPointerException exception

The I value in if (2==1&++i) increases, and the I value inif (2==1&&++i) does not increase.

& can also do bitwise-and operation

|| and | How to use and && and & empathy

The above is the details of & and && the whole content of the difference, I hope to help you.

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.