int compareTo(Boolean b)
描述 (Description)
java.lang.Boolean.compareTo(Boolean b)将此Boolean实例与另一个实例进行比较。
声明 (Declaration)
以下是java.lang.Boolean.compareTo()方法的声明
public int compareTo(Boolean b)
指定 (Specified by)
compareTo in interface Comparable《Boolean》
参数 (Parameters)
b - 要比较的布尔实例
返回值 (Return Value)
这个方法返回,
zero - 如果此对象表示与参数相同的布尔值
a positive value - 如果此对象表示true且参数表示false
a negative value - 如果此对象表示false且参数表示true。
异常 (Exception)
NullPointerException - 如果参数为null
例子 (Example)
以下示例显示了lang.Boolean.compareTo()方法的用法。
package com.iowiki;
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