With (&,&&) and OR (|,| |) The difference

Source: Internet
Author: User

1. With (&,&&) and OR (|,| |) The difference
1.1 When using and operating, it is necessary to ask the contents of several expressions to be true, the end result is true, and if one is false, the end result is false;
1.2 When using or operating, ask the front and back several expressions as long as there is a true, the end result is true, if all is false, then the final result is false;

2. Differences with (&) and Short circuits (&&) and/or (|) and short circuit or (| |) The difference

This is the beginner, the confused people will be more
First say and operation: Since the operation requires the content of several expressions before and after the final result is true, if the use of short-circuit and, as long as the first result is false, the overall result is definitely false, but the program at this time will not continue to execute, on the contrary, if not short-circuit and , even if the first result is false, the program will continue to perform other results, although the end result is the same, give an example, clear:

Java code
    1. public class test1{   
    2.     public  Static void main (string args[]) {  
    3.         if (10!=10&10/0==0) {  
    4.              system.out.println ( "condition satisfies")  ;  
    5.          }  
    6.     }  
    7. };  


Apparently, 10! =10 The result is false, but because there is no use of short-circuit with, the program will continue to execute the second condition result judgment, the second in the program obviously exception, so this code compiled can pass, but the operation will be error!
Look at the code again:
public class test2{
public static void Main (String args[]) {
if (10!=10&&10/0==0) {
SYSTEM.OUT.PRINTLN ("Condition meets");
}
}
};
The first result is false, the program will not be executed, so the program is normal operation;

Next Talk or (|) and short circuit or (| |) The difference
In fact, you understand the difference between the two before, this is very simple
Or it means that the expression as long as there is a true, the result is true, all expressions are false, the result is false;
Short-circuit and representation as long as the first expression is true, the program does not perform other expression judgments, and conversely, if it is not a short-circuit or, even if the first one is true, all other expressions are executed.

Java code
    1. Public class test3{
    2. public static void Main (String args[]) {
    3. if (10==10| 10/0==0) {
    4. System.out.println ("condition meets");
    5. }
    6. }
    7. };


Obviously, this program execution will error, because the program will judge each expression, and the following:

Java code
    1. Public class test4{
    2. public static void Main (String args[]) {
    3. if (10==10| | 10/0==0) {
    4. System.out.println ("condition meets");
    5. }
    6. }
    7. };


The above program because the first expression is true, and the use of a short-circuit or, the program will no longer perform the following judgment, all programs run normally;

With (&,&&) and OR (|,| |) The difference

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.