| And & no extra judgment

Source: Internet
Author: User
Check the code: public class testorinfo {
Public static Boolean compareit (int I) {if (I> 10) {system. out. println ("in the (I> 10)"); Return true;} else {system. out. println ("in the (I <= 10)");} return false;} public static void main (string [] ARGs) {If (compareit (111) | compareit (2) {// note that system. out. println ("OK ");}}}
/* Print the output result as follows: In the (I> 10) OK ================, when the first condition compareit (111) is determined to be true, compareit (15) is not executed ). Because | or operation, when a condition is true, you can determine this or operation, that is, the entire judgment condition is true. So there is no output as expected: In the (I <= 10) in the (I> 10) OK is the same as & RMB, when the result on the left is falses, the result on the right is not determined, because it is enough to judge that the entire & Meta is false.

*/Finally try to replace & with & and replace | with | and then run the above program. You will see different results. After you re-modify the program and run the program as mentioned above, you will find & | in fact, it is flexible and reduces the execution of statements according to the actual situation, as we can see in the program results, we advocate using & and | when making judgments, because of its high efficiency. Although & and | can also be implemented. Another thing to remember is that & and | can also perform bitwise operations, so it seems that & and | assume multiple roles.

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.