布爾類的compareTo()方法是Java中的一種內置方法,用於將給定的布爾實例與當前實例進行比較。
用法:
BooleanObject.compareTo(Boolean a)
參數:它采用布爾值a作為要與當前實例進行比較的參數。
返回類型:函數的返回類型為int。它返回:
- 如果“ a”等於“ b”,則為0,
- 如果“ a”為假而“ b”為真,則為負值,
- 如果“ a”為真而“ b”為假,則為正值。
下麵是說明布爾類的compareTo()方法的程序:
示例1:
// Java code to implement
// compareTo() method of Boolean class
class GeeksforGeeks {
// Driver method
public static void main(String[] args)
{
// Boolean object
Boolean a = new Boolean(true);
// Boolean object
Boolean b = new Boolean(true);
// compare method
System.out.println(a + " comparing with " + b
+ " = " + a.compareTo(b));
}
}
輸出:
true comparing with true = 0
示例2:
// Java code to implement
// compareTo() method of Java class
class GeeksforGeeks {
// Driver method
public static void main(String[] args)
{
// Boolean object
Boolean a = new Boolean(true);
// Boolean object
Boolean b = new Boolean(false);
// compare method
System.out.println(a + " comparing with " + b
+ " = " + a.compareTo(b));
}
}
輸出:
true comparing with false = 1
示例3:
// Java code to implement
// compareTo() method of Java class
class GeeksforGeeks {
// Driver method
public static void main(String[] args)
{
// Boolean object
Boolean a = new Boolean(false);
// Boolean object
Boolean b = new Boolean(true);
// compare method
System.out.println(a + " comparing with " + b
+ " = " + a.compareTo(b));
}
}
輸出:
false comparing with true = -1
相關用法
- Java Booleans.indexOf(boolean[] array, boolean target)用法及代碼示例
- Java Booleans.indexOf(boolean[] array, boolean[] target)用法及代碼示例
- Java UUID compareTo()用法及代碼示例
- Java Short compareTo()用法及代碼示例
- Java Calendar compareTo()用法及代碼示例
- Java Charset compareTo()用法及代碼示例
- Java DoubleBuffer compareTo()用法及代碼示例
- Java Path compareTo()用法及代碼示例
- Java ChronoZonedDateTime compareTo()用法及代碼示例
- Java LocalDateTime compareTo()用法及代碼示例
- Java ChronoLocalDateTime compareTo()用法及代碼示例
- Java OffsetDateTime compareTo()用法及代碼示例
- Java ByteBuffer compareTo()用法及代碼示例
- Java MonthDay compareTo()用法及代碼示例
- Java Double.compareTo()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 Boolean compareTo() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。