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