當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。