Java - Boolean logicalOr() 方法



描述

Java Boolean logicalOr() 方法返回將邏輯或運算子應用於指定的布林運算元的結果。

宣告

以下是 java.lang.Boolean.logicalOr(boolean a, boolean b) 方法的宣告

public static boolean logicalOr​(boolean a, boolean b)

引數

a − 第一個運算元

b − 第二個運算元

返回值

此方法返回 a 和 b 的邏輯或。

異常

在兩個值為 true 的布林值上計算邏輯或示例

以下示例演示了在 true 和 true 值上使用 Boolean logicalOr() 方法。在此程式中,我們建立了兩個布林變數併為其分配了 true 值。然後,我們使用 logicalOr() 方法對這兩個布林變數進行了邏輯或運算並獲取了結果。最後,列印結果。

package com.tutorialspoint;

public class BooleanDemo {
   public static void main(String[] args) {

      // create 3 boolean variables
      boolean b1, b2, result;
      
      // assign value to b1, b2
      b1 = true;
      b2 = true;
      
      // perform the logical OR operation
      result = Boolean.logicalOr(b1, b2);	  
      
      // print result
      System.out.println( "b1: " + b1 + " OR b2: " + b2 +", b1 OR b2: " + result );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

b1: true OR b2: true, b1 OR b2: true

在兩個布林值(一個為 true,一個為 false)上計算邏輯或示例

以下示例演示了在 true 和 false 值上使用 Boolean logicalOr() 方法。在此程式中,我們建立了兩個布林變數併為其分配了 true 和 false 值。然後,我們使用 logicalOr() 方法對這兩個布林變數進行了邏輯或運算並獲取了結果。最後,列印結果。

package com.tutorialspoint;

public class BooleanDemo {
   public static void main(String[] args) {

      // create 3 boolean variables
      boolean b1, b2, result;
      
      // assign value to b1, b2
      b1 = true;
      b2 = false;
      
      // perform the logical OR operation
      result = Boolean.logicalOr(b1, b2);	  
      
      // print result
      System.out.println( "b1: " + b1 + " OR b2: " + b2 +", b1 OR b2: " + result );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

b1: true OR b2: false, b1 OR b2: true

在兩個值為 false 的布林值上計算邏輯或示例

以下示例演示了在 false 和 false 值上使用 Boolean logicalOr() 方法。在此程式中,我們建立了兩個布林變數併為其分配了 false 值。然後,我們使用 logicalOr() 方法對這兩個布林變數進行了邏輯或運算並獲取了結果。最後,列印結果。

package com.tutorialspoint;

public class BooleanDemo {
   public static void main(String[] args) {

      // create 3 boolean variables
      boolean b1, b2, result;
      
      // assign value to b1, b2
      b1 = false;
      b2 = false;
      
      // perform the logical OR operation
      result = Boolean.logicalOr(b1, b2);	  
      
      // print result
      System.out.println( "b1: " + b1 + " OR b2: " + b2 +", b1 OR b2: " + result );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

b1: false OR b2: false, b1 OR b2: false
java_lang_boolean.htm
廣告