&& and & in Java | | and | The difference between

Source: Internet
Author: User

In Java's logical operators, there are so four classes of:&& (short-circuit and),& (with), | (or), | | (short circuit or).

&& and & are both expressed and, the difference is && as long as the first condition is met, the later conditions are no longer judged. & has to judge all the conditions.

"&" Action

The code is as follows:

  1. Public class operatedemo01{

  2. /**

  3. Verify (&) and action

  4. Description: The "and" operation requires all conditions to be judged again

  5. */

  6. Public Static void Main (String args[]) {

  7. if (ten!= &/0= =0) {

  8. /*10! =10 this condition returns false

  9. 10/0==0 This place will throw an exception (java.lang.ArithmeticException:/by zero)

  10. Due to the need to determine all conditions of operation, there will be errors at 10/0

  11. */

  12. System.out.print ("conditions meet!") ");

  13. }

  14. }

  15. }

Operation Result:


Modify the following code:

"&&" Action:

  1. Public class operatedemo02{

  2. /**

  3. Verifying the "Short circuit and &&" operation

  4. Description: "Short circuit and &&" operation if the first condition is not satisfied then the following conditions will not be judged.

  5. */

  6. Public Static void Main (String args[]) {

  7. if (ten!= &&/0= =0) {

  8. /*10! =10 this condition returns false

  9. Because the first condition is not satisfied, so the 10/0==0 this condition no longer need to judge

  10. */

  11. System.out.print ("conditions meet!") ");

  12. }

  13. }

  14. }

Operation Result:

|| (short-circuit or) and | (or) are all "or", the difference is | | As long as the first condition is met, the following conditions are no longer judged, and | All conditions are judged.

Look at the following code:

"| |" Operation

  1. Public class operatedemo03{

  2. /**

  3. Verify the "short-circuit or | |" Operation

  4. Description: "Short Circuit or | |" If the first condition is true then the following conditions are no longer judged

  5. */

  6. Public Static void Main (String args[]) {

  7. if (ten =Ten| | 0= =0) {

  8. /*10==10 This condition returns true

  9. Because the first condition is satisfied, the condition of 10/0==0 no longer has to be judged.

  10. */

  11. System.out.print ("conditions meet!") ");

  12. }

  13. }

  14. }

Operation Result:


Modify the following code:

| Operation

  1. Public class operatedemo04{

  2. /**

  3. Verify the OR | action

  4. Description: "or |" All conditions require judgment

  5. */

  6. Public Static void Main (String args[]) {

  7. if (ten =Ten| 0= =0) {

  8. /*

  9. Although 10==10 returns true but 10/0==0 this condition will throw an exception error

  10. So when the second condition is judged, it throws an exception.

  11. */

  12. System.out.print ("conditions meet!") ");

  13. }

  14. }

  15. }

Operation Result:



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.