📜  Java中的布尔型 equals() 方法及示例

📅  最后修改于: 2022-05-13 01:55:06.298000             🧑  作者: Mango

Java中的布尔型 equals() 方法及示例


Boolean 类的equals()方法是Java的内置方法,用于检查两个布尔对象的相等性。

句法:

BooleanObject.equals(Object ob)

参数:它以Object类型的参数ob作为输入,它是要比较的实例。

返回类型:返回类型为boolean 。如果指定的对象“ob”与“布尔对象”具有相同的值,则返回 true,否则返回 false。

下面是说明 Boolean 类的 equals() 方法的程序:

方案一:

// 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

方案二:

// 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