当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Boolean equals()用法及代码示例



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

用法:

BooleanObject.equals(Object ob)

参数:它采用对象类型的参数ob作为输入,这是要比较的实例。


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

下面是说明布尔类的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


相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 Boolean equals() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。