java的邏輯運算子

來源:互聯網
上載者:User

邏輯運算子AND(&&)、OR(||)以及NOT(!)能產生一個布爾值(true或false)——以自變數的邏輯關係為基礎。下面這個例子向大家展示了如何使用關係和邏輯運算子。

 

//: Bool.java// Relational and logical operatorsimport java.util.*;public class Bool {  public static void main(String[] args) {    Random rand = new Random();    int i = rand.nextInt() % 100;    int j = rand.nextInt() % 100;    prt("i = " + i);    prt("j = " + j);    prt("i > j is " + (i > j));    prt("i < j is " + (i < j));    prt("i >= j is " + (i >= j));    prt("i <= j is " + (i <= j));    prt("i == j is " + (i == j));    prt("i != j is " + (i != j));    // Treating an int as a boolean is     // not legal Java//! prt("i && j is " + (i && j));//! prt("i || j is " + (i || j));//! prt("!i is " + !i);    prt("(i < 10) && (j < 10) is "       + ((i < 10) && (j < 10)) );    prt("(i < 10) || (j < 10) is "       + ((i < 10) || (j < 10)) );  }  static void prt(String s) {    System.out.println(s);  }} ///:~


只可將AND,OR或NOT應用於布爾值。與在C及C++中不同,不可將一個非布爾值當作布爾值在邏輯運算式中使用。若這樣做,就會發現嘗試失敗,並用一個“//!”標出。然而,後續的運算式利用關係比較產生布爾值,然後對結果進行邏輯運算。
輸出資料行表看起來象下面這個樣子:

 

i = 85j = 4i > j is truei < j is falsei >= j is truei <= j is falsei == j is falsei != j is true(i < 10) && (j < 10) is false(i < 10) || (j < 10) is true


注意若在預計為String值的地方使用,布爾值會自動轉換成適當的文本形式。
在上述程式中,可將對int的定義替換成除boolean以外的其他任何主要資料類型。但要注意,對浮點數字的比較是非常嚴格的。即使一個數字僅在小數部分與另一個數字存在極微小的差異,仍然認為它們是“不相等”的。即使一個數字只比零大一點點(例如2不停地開平方根),它仍然屬於“非零”值。

1. 短路
操作邏輯運算子時,我們會遇到一種名為“短路”的情況。這意味著只有明確得出整個運算式真或假的結論,才會對錶達式進行邏輯求值。因此,一個邏輯運算式的所有部分都有可能不進行求值:

 

//: ShortCircuit.java// Demonstrates short-circuiting behavior// with logical operators.public class ShortCircuit {  static boolean test1(int val) {    System.out.println("test1(" + val + ")");    System.out.println("result: " + (val < 1));    return val < 1;  }  static boolean test2(int val) {    System.out.println("test2(" + val + ")");    System.out.println("result: " + (val < 2));    return val < 2;  }  static boolean test3(int val) {    System.out.println("test3(" + val + ")");    System.out.println("result: " + (val < 3));    return val < 3;  }  public static void main(String[] args) {    if(test1(0) && test2(2) && test3(2))      System.out.println("expression is true");    else      System.out.println("expression is false");  }} ///:~


每次測試都會比較自變數,並返回真或假。它不會顯示與準備調用什麼有關的資料。測試在下面這個運算式中進行:
if(test1(0)) && test2(2) && test3(2))
很自然地,你也許認為所有這三個測試都會得以執行。但希望輸出結果不至於使你大吃一驚:

 

if(test1(0) && test2(2) && test3(2))

第一個測試產生一個true結果,所以運算式求值會繼續下去。然而,第二個測試產生了一個false結果。由於這意味著整個運算式肯定為false,所以為什麼還要繼續剩餘的運算式呢?這樣做只會徒勞無益。事實上,“短路”一詞的由來正種因於此。如果一個邏輯運算式的所有部分都不必執行下去,那麼潛在的效能提升將是相當可觀的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.