Short-circuiting (&) in Java with (&&) and OR (|) Short circuit or (| | ) of the relationship

Source: Internet
Author: User

First, the use of logical operators

1) The logical operator is a Boolean expression that is to be distinguished from the bitwise operator.

2) How to use:

public class Test {
public static void Main (string[] args) {
System.out.println (True & false);//result is False
System.out.println (True & True);//result is true
System.out.println (False & False);//result is False
System.out.println (False & True);//result is False
/*
System.out.println (True && false);//result is False
System.out.println (True && true);//result is true
System.out.println (False && false);//result is False
System.out.println (False && true);//result is False
*/
}

}

Conclusion: Logic is true only if the expression on both sides of the symbol is true

==============================================================

public class Test {
public static void Main (string[] args) {
System.out.println (True | false);//result is true
System.out.println (True | true);//result is true
System.out.println (False | false);//result is False
System.out.println (False | true);//result is true
/*
System.out.println (True | | false);//result is true
System.out.println (True | | true);//result is true
System.out.println (False | | false);//result is False
System.out.println (False | | true);//result is true
*/
}

}

Conclusion: A logical or false result only if the expression on either side of the symbol is false

==============================================================

Second, ' short circuit and ', ' short circuit or '

1) ' &, | ' With ' &&, | | ' The difference

A single logical operator evaluates the left and right two expressions to derive a Boolean value and then perform the operation. ' Short circuit and ' if the left expression is false, the right expression will not be judged,

Because the result must be false, a ' short-circuit or ' if the left-hand expression evaluates to true does not judge the right expression because the result must be true.

The logical operator of a phrase is more efficient than a normal logical operator.

Iii. examples

public class Test {
public static void Main (string[] args) {

int num = 1;
System.out.println (False & Num++==1);//Even if the front is false, the expression on the right will continue to execute, i.e. num++
SYSTEM.OUT.PRINTLN (num);//2
System.out.println ("===== Gorgeous split line =====");
num = 1;
System.out.println (False && num++==1);//The left expression is false and the right expression will not execute, that is, Num will not increment
SYSTEM.OUT.PRINTLN (num);//1

}

}

The result of the input is:

False
2
===== Gorgeous split-line =====
False
1

Short circuit and is the same reason, you can write your own code verification

==============================================================

Short-circuiting (&) in Java with (&&) and OR (|) Short circuit or (| | ) of the relationship

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.