Java Operators general Operation rules

Source: Internet
Author: User
Tags bitwise expression

One disadvantage of using operators is that the parentheses are often easily mistaken. It is easy to confuse the use of parentheses even if there is a slightest uncertainty about how an expression is evaluated. This problem still exists in Java.
In C and C + +, a particularly common error is as follows:

while (x = y) {
//...
}

The intention of the program is to test for "equal" (= =), rather than for assignment operations. In C and C + +, if Y is a non 0 value, then the result of this assignment is definitely true. This makes it possible to get an infinite loop. In Java, the result of this expression is not a Boolean value, and the compiler expects a Boolean value that is not converted from an int value. So when compiling, the system prompts for an error that effectively prevents us from running the program further. So this shortcoming in Java will never cause more serious consequences. The only thing that does not get compilation errors is that both X and Y are Boolean values. In this case, x = y is a valid expression. In this case, it could be an error.
In C and C + +, a similar problem is to use bitwise AND and OR, rather than logical and and OR. Use one of two characters (& or |) by bitwise AND And OR, while logical and and or using two identical characters (&& or | | )。 Just like "=" and "=", typing a character is certainly easier than typing two. In Java, the compiler can also prevent this, because it does not allow us to force a type that does not belong.

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.