Guava庫中布爾類的compare()方法用於比較兩個指定的布爾值。這些值作為參數傳遞,比較結果作為第一值和第二值之差得到。因此,它可以為正,零或負。
用法:
public static int compare(boolean a, boolean b)
參數:此方法接受兩個參數:
- a:這是要比較的第一個布爾對象。
- b:這是要比較的第二個布爾對象。
返回值:此方法返回一個int值。它返回:
- 如果“ a”等於“ b”,則為0,
- 如果“ a”為真而“ b”為假,則為正值,
- 如果“ a”為假而“ b”為真,則為負值
異常:該方法不會引發任何異常。
以下示例程序旨在說明Booleans.compare()方法:
範例1:
// Java code to show implementation of
// Guava's Booleans.compare() method
import com.google.common.primitives.Booleans;
class GFG {
public static void main(String[] args)
{
boolean a = true;
boolean b = true;
// compare method in Booleans class
int output = Booleans.compare(a, b);
// printing the output
System.out.println("Comparing " + a
+ " and " + b + ":"
+ output);
}
}
輸出:
Comparing true and true:0
範例2:
// Java code to show implementation of
// Guava's Booleans.compare() method
import com.google.common.primitives.Booleans;
class GFG {
public static void main(String[] args)
{
boolean a = true;
boolean b = false;
// compare method in Booleans class
int output = Booleans.compare(a, b);
// printing the output
System.out.println("Comparing " + a
+ " and " + b + ":"
+ output);
}
}
輸出:
Comparing true and false:1
範例3:
// Java code to show implementation of
// Guava's Booleans.compare() method
import com.google.common.primitives.Booleans;
class GFG {
public static void main(String[] args)
{
boolean a = false;
boolean b = true;
// compare method in Booleans class
int output = Booleans.compare(a, b);
// printing the output
System.out.println("Comparing " + a
+ " and " + b + ":"
+ output);
}
}
輸出:
Comparing false and true:-1
相關用法
- Java Guava Doubles.min()用法及代碼示例
- Java Guava Doubles.contains()用法及代碼示例
- Java Guava Floats.contains()用法及代碼示例
- Java Guava Bytes.contains()用法及代碼示例
- Java Guava Longs.contains()用法及代碼示例
- Java Guava Shorts.min()用法及代碼示例
- Java Guava Longs.min()用法及代碼示例
- Java Guava Chars.min()用法及代碼示例
- Java Guava Floats.min()用法及代碼示例
- Java Guava Longs.max()用法及代碼示例
- Java Guava Doubles.max()用法及代碼示例
- Java Guava Chars.max()用法及代碼示例
- Java Guava Floats.max()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
- Java Guava Shorts.contains()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | Booleans.compare() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。