The difference between & & and &, | and | in Java logic operators

There are four types of logical operators in Java: & & (short circuit and), &, |, | (short circuit or).

&&And & both mean and. The difference is & & as long as the first condition is not satisfied, the latter condition will not be judged. And & judge all the conditions.

See the following procedure:

The output is "no error". Changing & & to & will result in the following error:

The reason is: & &, the first condition is judged as false, and the following 100 / 0 = = 0 condition is not judged.

&All the conditions should be judged during operation, so the following conditions will be judged, so an error will be reported.

||And | both mean "or". The difference is that as long as the first condition is met, the latter condition will no longer be judged, but all conditions should be judged.

See the following procedure:

At this time, the output "operation is OK". If | is changed to |, an error will be reported.

The reason is: | judge that the first condition is true, the following conditions execute the code in parentheses without judgment, and | judge all conditions, so an error will be reported.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>