布尔类的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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。