Java - Boolean compareTo() 方法



描述

Java Boolean compareTo(Boolean b) 方法用於比較此 Boolean 例項與另一個例項。

宣告

以下是 java.lang.Boolean.compareTo() 方法的宣告

public int compareTo(Boolean b)

指定於

介面 Comparable<Boolean> 中的 compareTo

引數

b − 要比較的 Boolean 例項

返回值

此方法返回:

  • − 如果此物件表示與引數相同的布林值

  • 正值 − 如果此物件表示 true 且引數表示 false

  • 負值 − 如果此物件表示 false 且引數表示 true。

異常

NullPointerException − 如果引數為 null

比較 true 和 false 布林值物件的示例

以下示例演示瞭如何使用 Boolean compareTo() 方法與 Boolean 物件。在此程式中,我們建立了兩個 Boolean 變數,並分別為它們分配了值為 true 和 false 的 Boolean 物件。使用 compareTo() 方法比較這兩個物件,並列印結果。

package com.tutorialspoint;

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

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = Boolean.valueOf(true);
      b2 = Boolean.valueOf(false);

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

輸出

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

Object value is true

比較 true 和 false 布林值基本型別的示例

以下示例演示瞭如何使用 Boolean compareTo() 方法與 Boolean 物件。在此程式中,我們建立了兩個 Boolean 變數,並分別為它們分配了值為 true 和 false 的布林值基本型別。使用 compareTo() 方法比較這兩個物件,並列印結果。

package com.tutorialspoint;

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

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = true;
      b2 = false;

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

輸出

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

Object value is true

比較 true 和 false 布林值物件的示例

以下示例演示瞭如何使用 Boolean compareTo() 方法與 Boolean 物件。在此程式中,我們建立了兩個 Boolean 變數,一個分配了值為 true 的布林值基本型別,另一個分配了值為 false 的 Boolean 物件。使用 compareTo() 方法比較這兩個物件,並列印結果。

package com.tutorialspoint;

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

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = true;
      b2 = Boolean.valueOf(false);

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

輸出

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

Object value is true
java_lang_boolean.htm
廣告