Java short-circuit operator and non-short circuit operator

xiaoxiao2021-03-06  51

The short circuit operator is our common "&&", "||", generally referred to as "conditional operation."

Class logic {public ststic void main (string [] args) {INT A = 1; int b = 1; if (a

"&&" operator checks if the first expression returns "false", if it is "false", the result will be "false" and no longer check the other content.

"A / 0" is an obvious mistake! However, the short circuit operation "&&" first judges "a

Class logic {public ststic void main (string [] args) {INT A = 1; int b = 1; if (a == B || B

"||" operator checks if the first expression returns "True", if it is "true", the result will be "true", no longer checking other content.

"A / 0" is an obvious mistake! However, short-circuit operation "||" first executed "A == B" judgment, return "True", resulting in short circuit, and does not perform "A / 0" operation, the program will play "That's in my control.". At this time, exchange the expression on the left and right sides of "||", and the program immediately throws an exception "java.lang.arithmeticexception: / by zero".

Non-short-circuit operators include "&", "| or", "^ vary or", is generally called "logical operation"

Class logic {public ststic void main (string [] args) {int a = 1; int b = 1; if (a

The "&" operator will not cause short circuits, which will seriously check each expression, although "a

Similarly, "|" operator does not cause short circuit, although "a == b" has returned "True", it will continue to check other content, so that it will eventually throw an exception "java.lang.arithmeticexception: / By Zero.

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

At last. Short-circuit operators can only be used in logical expressions, and non-short-circuit operators can be used in bit expressions and logical expressions. It can also be said that short circuit operations can only operate Boolean, not only to manipulate Boolean, but also operate value. (Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)


New Post(0)