"Go" Java in,&& and &,| | The difference from |

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/lishiyuzuji/article/details/8116516

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

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

Look at the following program:

[HTML]View plain copy print?
  1. public static void Main (string[] args) {
  2. TODO auto-generated Method Stub
  3. if ((23!=23) && (100/0==0)) {
  4. System.out.println ("The operation is not a problem. ");
  5. }else{
  6. <span style="White-space:pre"> </span>system.out.println ("no Error");
  7. }
  8. }

The output is "no error." Instead of changing && to &, the error will be as follows:

[HTML]View plain copy print?
    1. Exception in thread "main" java.lang.ArithmeticException:/By zero


The reason is:&& when the first condition is false, the latter 100/0==0 the condition is not judged.
& to judge all the conditions, so the following conditions will be judged, so will error.




|| and | are all expressions "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 program:

[HTML]View plain copy print?
  1. public static void Main (string[] args) {
  2. TODO auto-generated Method Stub
  3. if ((23==23) | | | (100/0==0)) {  
  4. System.out.println ("The operation is not a problem. ");
  5. }else{
  6. <span style="White-space:pre"> </span>system.out.println ("no Error");
  7. }
  8. }

The output "operation is no problem" at this time. If the | | Instead, the error will be.


The reasons are: | | Judging the first condition is true, the following conditions do not judge the code in parentheses, and | To judge all the conditions,
So the error will be.

"Go" Java in,&& and &,| | The difference from |

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.