Java BooleanObject equals()方法及示例
布尔类的 equals() 方法是Java中的一个内置方法,用于检查两个布尔对象的平等性。
语法
BooleanObject.equals(Object ob)
参数: 它接受一个Object类型的参数ob作为输入,它是要比较的实例。
返回类型: 返回类型是 布尔型 如果指定的对象’ob’与’BooleanObject’的值相同,则返回真,否则返回假。
下面的程序说明了布尔类的equals()方法。
程序1 :
// Java code to implement
// equals() 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.equals(b));
}
}
输出:
true comparing with true = true
程序2
// Java code to implement
// equals() 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.equals(b));
}
}
输出:
true comparing with false = false
程序3
// Java code to implement
// equals() 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.equals(b));
}
}
输出:
false comparing with true = false