布爾類的compare()方法是Java中的內置方法,用於比較兩個布爾值。這是一個靜態方法,因此可以在不創建布爾類的任何對象的情況下直接調用它,即直接使用類名。
用法:
Boolean.compare(boolean a, boolean b)
參數:它在參數中采用兩個布爾值a和b進行比較。
返回類型:函數的返回類型為int。它返回
- 如果“ a”等於“ b”,則為0,
- 如果“ a”為假而“ b”為真,則為負值,
- 如果“ a”為真而“ b”為假,則為正值。
下麵是說明布爾類的compare()方法的程序:
示例1:
// Java code to implement
// compare() method of Boolean class
class GeeksforGeeks {
// Driver method
public static void main(String[] args)
{
// first value
boolean a = true;
// second value
boolean b = true;
// compare method
System.out.println(a + " comparing with " + b
+ " = " + Boolean.compare(a, b));
}
}
輸出:
true comparing with true = 0
示例2:
// Java code to implement
// compare() method of Java class
class GeeksforGeeks {
// Driver method
public static void main(String[] args)
{
// first value
boolean a = true;
// second value
boolean b = false;
// compare method
System.out.println(a + " comparing with " + b
+ " = " + Boolean.compare(a, b));
}
}
輸出:
true comparing with false = 1
示例3:
// Java code to implement
// compare() method of Java class
class GeeksforGeeks {
// Driver method
public static void main(String[] args)
{
// first value
boolean a = false;
// second value
boolean b = true;
// compare method
System.out.println(a + " comparing with " + b
+ " = " + Boolean.compare(a, b));
}
}
輸出:
false comparing with true = -1
相關用法
- Java Booleans.indexOf(boolean[] array, boolean[] target)用法及代碼示例
- Java Booleans.indexOf(boolean[] array, boolean target)用法及代碼示例
- Java Double compare()用法及代碼示例
- Java Float compare()用法及代碼示例
- Java Byte compare()用法及代碼示例
- Java Guava Longs.compare()用法及代碼示例
- Java Guava Ints.compare()用法及代碼示例
- Java Guava Chars.compare()用法及代碼示例
- Java Guava Doubles.compare()用法及代碼示例
- Java Guava Floats.compare()用法及代碼示例
- Java Guava Booleans.compare()用法及代碼示例
- Java Boolean compareTo()用法及代碼示例
- Java Boolean parseBoolean()用法及代碼示例
- Java Boolean booleanValue()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 Boolean compare() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。