1. 程式人生 > >Java基礎-邏輯運算符Logic Operators

Java基礎-邏輯運算符Logic Operators

ron public amp color AI pos tor out 作者

              Java基礎-邏輯運算符Logic Operators

                                作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

一.邏輯運算符

  邏輯運算符是對布爾值進行操作運算的,常見的有:

    1>.邏輯與(&);

    2>.邏輯或(|);

    3>.邏輯異或(^);

    4>.邏輯非(!);

    5>.短路與(&&);

    6>.短路或(||);

技術分享圖片

  總結規律如下:

    1>."&":只有兩個操作數都是true,結果才是true,其余都是false;


    2>."|":只要有一個操作數是true,結果就是true,其余都是false;
    3>."!":取反,true變false,false變true;
    4>.單操作數的只有一個;
    5>.異或的含義:求異,只有兩個操作數不同,整個表達式才為true;

二.案例展示

1>.邏輯運算符的操作數與布爾值

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
5 */ 6 7 public class LogicDome{ 8 public static void main(String[] args){ 9 10 //邏輯運算符的操作數與布爾值 11 System.out.println( true & true ); //true 12 System.out.println( false | true ); //true 13 System.out.println( true ^ false ); //
true 14 System.out.println( true ^ true ); //false 15 System.out.println( !true ); //false 16 System.out.println( !false ); //true 17 18 } 19 }

2>.短路與&&,操作結果與邏輯&一樣(推薦使用)

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 public class LogicDome2{
 8     public static void main(String[] args){
 9         
10         //短路與&&,操作結果與邏輯&一樣
11         int num = 2018;
12         System.out.println( (num < 0) & ( ++num > 0) );        //false
13         System.out.println( num );                                //2019
14         
15         //短路與&&,如果左側表達式為false,不計算右側表達式的值了
16         num = 18;
17         System.out.println( (num < 0) && ( ++num > 0) );        //false
18         System.out.println( num );                                //18
19     }
20 }

3>.短路或||,如果左側為true,不計算右側表達式的值了(推薦使用)

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 public class LogicDome3{
 8     public static void main(String[] args){
 9         
10         //短路與||,如果左側為true,不計算右側表達式的值了
11         int num = 2018;
12         System.out.println( (num > 0) || ( ++num > 0) );        //false
13         System.out.println( num );                                //2018
14         
15     }
16 }

4>.邏輯運算符經常用於鏈接多個布爾表達式

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 public class LogicDome4{
 8     public static void main(String[] args){
 9         
10         //邏輯運算符經常用於鏈接多個布爾表達式
11         int year = 2018;    
12 
13         /**
14         判斷某一年是否為閏年:
15             能被4整除但是不能被100整除                    ||     直接能被400整除
16             (能被4整除 && 不能被100整除)             ||     直接能被400整除
17             (year%4==0 &&  year%100!=0)    ||    year%400==0;
18         */
19         
20         boolean isPrime = (year%4==0 &&  year%100!=0)    ||    year%400==0;        //false
21     
22         System.out.println( isPrime);
23         
24     }
25 }

Java基礎-邏輯運算符Logic Operators