Java Foundation-logical operator Logic Operators

Source: Internet
Author: User
Tags logical operators

Java Foundation-logical operator Logic Operators

Yun Zhengjie

Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.

I. Logical operators

Logical operators operate on Boolean values, which are common:

1> logic and (&);

2> logical OR (|);

3>. Logical XOR (^);

4>. logical non (!);

5>: Short circuit and (&&);

6> Short circuit or (| | );

The following rules are summarized:

1>. " & ": Only two operands are true, the result is true, the rest is false;
2>. "|" : As long as one operand is true, the result is true and the rest is false;
3>. "! ": Inverse, true changes false,false to true;
4>. Only one of the single operands;
5> The meaning of XOR: To make a difference, only two operands are different, the entire expression is true;

Two. Case Show

1>: Operands and Boolean values for logical operators

1 /*2 @author: Yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/4 Email:[email protected]5 */6 7  Public classlogicdome{8      Public Static voidMain (string[] args) {9         Ten         //operands and Boolean values for logical operators OneSystem.out.println (true&true);//true ASystem.out.println (false|true);//true -System.out.println (true^false);//true -System.out.println (true^true);//false theSYSTEM.OUT.PRINTLN (!true);//false -SYSTEM.OUT.PRINTLN (!false);//true -          -     } +}

2>: Short circuit and &&, operation result same as Logic & (recommended)

1 /*2 @author: Yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/4 Email:[email protected]5 */6 7  Public classlogicdome2{8      Public Static voidMain (string[] args) {9         Ten         //Short Circuit and &&, operation result is same as logic & One         intnum = 2018; ASYSTEM.OUT.PRINTLN (num < 0) & (++num > 0));//false -SYSTEM.OUT.PRINTLN (num);//2019 -          the         //short-circuit and && If the left expression is false, the value of the right expression is not evaluated -num = 18; -SYSTEM.OUT.PRINTLN (num < 0) && (++num > 0));//false -SYSTEM.OUT.PRINTLN (num);// - +     } -}

3> short-circuit or | |, if the left is true, the value of the right expression is not evaluated (recommended)

1 /*2 @author: Yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/4 Email:[email protected]5 */6 7  Public classlogicdome3{8      Public Static voidMain (string[] args) {9         Ten         //short-circuit with | |, if the left is true, the value of the right expression is not evaluated One         intnum = 2018; ASYSTEM.OUT.PRINTLN (num > 0) | | (++num > 0));//false -SYSTEM.OUT.PRINTLN (num);//2018 -          the     } -}

4>: Logical operators are often used to link multiple Boolean expressions

1 /*2 @author: Yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/4 Email:[email protected]5 */6 7  Public classlogicdome4{8      Public Static voidMain (string[] args) {9         Ten         //logical operators are often used to link multiple Boolean expressions One         intYear = 2018;  A  -         /** - determine whether a year is a leap years: the can be divisible by 4 but not divisible by 100 | | Can be divisible by 400 directly . - (Can be divisible by 4 && not divisible by 100) | | Can be divisible by 400 directly . - (year%4==0 && year%100!=0) | | year%400==0; -         */ +          -         BooleanIsPrime = (year%4==0 && year%100!=0) | | year%400==0;//false +      A System.out.println (isprime); at          -     } -}

Java Foundation-logical operator Logic Operators

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.