JAVA logical operators & amp; | difference

Source: Internet
Author: User

Logical Operator

Logical operators only operate on Boolean operands and return a boolean data. There are 6 logical operators: &, |, &, | ,! And ^

Short Circuit logic operator Short-Circuit Logical Operators:

Public class Lesson04_6 {

02 public static void main (String [] args ){

03 int I = 5;
04 // short circuit and operator &. true is returned only when both the left and right expressions are true. If the first expression on the left is false, false is returned immediately, return immediately as if it was a short circuit, saving some unnecessary computing time.
05 boolean flag = (I <3) & (I <4 );
06 System. out. println (flag );


08 // short circuit or operator |. If either of the two expressions on the left or right is true, true is returned. If the first expression on the left is true, true is returned immediately, return immediately as if it was a short circuit, saving some unnecessary computing time.
09 flag = (I> 4) | (I> 3 );
10 System. out. println (flag );
11}
12}


Non-Short-Circuit logical operator Not Short-Circuit Operators:

View sourceprint? 01 public class Lesson04_6 {

02 public static void main (String [] args ){

03 int I = 5;
04 // non-short circuit and operator &. true is returned only when both the left and right expressions are true. Both expressions are calculated.
05 boolean flag = (I <3) & (I <4 );
06 System. out. println (flag );


08 // non-short circuit or operator |. If either of the two expressions is true, true is returned. Both expressions are calculated.
09 flag = (I> 4) | (I> 3 );
10 System. out. println (flag );
11}

12}

Author: "the sky of blue flowers"

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.