Java short-circuit operators and non-short-circuit Operators

Source: Internet
Author: User
Basic

Short-circuit operators are commonly used "&" and "|", which are generally called "Conditional operations ".

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". If it is "false", the result must be "false" and no other content is checked.
"A/0" is an obvious error! However, when the short-circuit operation "&" first judges "A <B" and returns "false", it causes a short circuit and does not perform the "A/0" operation, the program will output "that's in my control. ". In this case, the program immediately throws an exception "Java. Lang. arithmeticexception:/by zero" in the exchange of expressions on both sides ".

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!!!");
}
}
}

"|" Operator checks whether the first expression returns "true". If it is "true", the result must be "true" and no other content is checked.
"A/0" is an obvious error! However, the short-circuit computation "|" first executes "A = B" and returns "true", which causes a short-circuit and does not perform the "A/0" operation, the program will output "that's in my control. ". In this case, the program immediately throws an exception "Java. Lang. arithmeticexception:/by zero" in the exchange of expressions between the left and right sides ".

Non-short-circuit operators include "&", "|", "^", and are generally called "logical operations"

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 will not cause short circuits. It will seriously check every expression. Although "A <B" has returned "flase", it will continue to check other content, so that an exception "Java. lang. arithmeticexception:/by zero ".

/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, the "|" operator does not cause short circuits. Although "A = B" has returned "true", it will continue to check other content, so that an exception "Java. lang. arithmeticexception:/by zero ".

The "^" operator is the same, so it is not here.

Last. Short-circuit operators can only be used in logical expressions. Non-short-circuit operators can be used in BIT expressions and logical expressions. It can also be said that the short-circuit operation can only operate on the boolean type, rather than the short-circuit operation can operate not only on the boolean type, but also on the numeric type.

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.