Java.lang.Boolean.compareTo() 方法
描述
java.lang.Boolean.compareTo(Boolean b) 将此布尔实例与另一个进行比较。
声明
以下是 java.lang.Boolean.compareTo() 方法的声明。
public int compareTo(Boolean b)
指定者
接口 Comparable<Boolean> 中的 compareTo
参数
b − 要比较的布尔实例
返回值
This method returns,
zero − 如果此对象表示与参数相同的布尔值
a positive value − 如果这个对象代表真而参数代表假
a negative value − 如果此对象表示 false 而参数表示 true。
异常
NullPointerException − 如果参数为空
示例
下面的例子展示了 lang.Boolean.compareTo() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class BooleanDemo {
public static void main(String[] args) {
// create 2 Boolean objects b1, b2
Boolean b1, b2;
// assign values to b1, b2
b1 = new Boolean(true);
b2 = new Boolean(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

