Java && | | Short-circuit explanation

Source: Internet
Author: User

Short-circuit operators are our common "&&", "| |", commonly referred to as "conditional actions."

Class logic{
Public ststic void Main (string[] args) {
int a=1;
int b=1;
if (a<b && b<a/0) {
System.out.println ("Oh,that ' s Impossible!!!");
}else{
System.out.println ("That's in my control.");
}
}
}

The


     && operator checks whether the first expression returns "false" and, if false, the result must be "false" and no longer checks for additional content.
"a/0" is an obvious mistake! However, the short-circuit operation "&&" first Judge "A<b", return "false", resulting in a short-circuit, will not be "a/0" operation, the program will be typed "that's in my control." This time, swap "&&" on both sides of the expression, the program immediately throws an exception "Java.lang.ArithmeticException:/By Zero".

class logic{
    public ststic void Main (string[] args) {
         int a=1;
        int b=1;
        if (a==b | | b<a/0) {
             system.out.println ("That's in my control.");
        }else{
             system.out.println ("Oh,that ' s Impossible!!!");
        }
    }
}


"| |" The operator checks whether the first expression returns "true" and, if "true", the result must be "true" and no longer checks for additional content.
"A/0" is an obvious mistake! But the short circuit operation "| |" Perform "a==b" judgment first, return "true", resulting in a short circuit, will not be "a/0" operation, the program will be typed "that's in my control." This time, exchange "| |" Right and left side of the expression, the program immediately throws an exception "Java.lang.ArithmeticException:/By Zero".


Non-short-circuit operators include "& and", "| or "," ^ xor ", commonly referred to as" logical operation "

class logic{
    public ststic void Main (string[] args) {
         int a=1;
        int b=1;
        if (A<b & b<a/0) {
             system.out.println ("Oh,that ' s Impossible!!!");
        }else{
             system.out.println ("That's in my control.");
        }
    }
}

The


     & operator does not cause a short-circuit, it will seriously check each expression, although "a<b" has returned "flase", it will continue to check other content, so that eventually throws an exception " Java.lang.ArithmeticException:/By Zero ".
    

Class logic{
Public ststic void Main (string[] args) {
int a=1;
int b=1;
if (a==b | b<a/0) {
System.out.println ("That's in my control.");
}else{
System.out.println ("Oh,that ' s Impossible!!!");
}
}
}


Similarly, "|" Operator also does not cause a short-circuit, although "a==b" has returned "true", it will continue to check other content so that it eventually throws an exception "Java.lang.ArithmeticException:/By Zero".

The "^" operator is the same, it's not here, Russell.

At last. Short-circuit operators can only be used in logical expressions, and non-short-circuiting operators can be used in bit expressions and logical expressions. It can also be said that the short-circuit operation can only operate the Boolean type, and not short-circuit operation can not only operate the Boolean type, and can manipulate the numerical type.

Reproduced in http://blog.sina.com.cn/s/blog_64e5287101012f9l.html

Java && | | Short-circuit explanation

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.