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


Java CompositeName equals()用法及代碼示例


javax.naming.CompositeName類的equals()方法用於將此CompositeName與作為參數傳遞的指定對象進行比較,並檢查兩個對象是否相等。如果兩個對象相等,則equals()方法返回true,否則返回false。如果傳遞的obj為null或不是複合名稱,則該方法返回false。如果一個組件中的每個組件與另一個組件中的相應組件相等,則兩個複合對象相等。

用法:

public boolean equals(Object obj)

參數:此方法接受obj,它是要比較的可能為null的對象。



返回值:如果obj等於此複合名稱,則此方法返回true,否則返回false。

下麵的程序說明CompositeName.equals()方法:
程序1:

// Java program to demonstrate 
// CompositeName.equals() 
  
import javax.naming.CompositeName; 
import javax.naming.InvalidNameException; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InvalidNameException 
    { 
  
        // create Composite name object 
        CompositeName compositeName1 
            = new CompositeName("x/y/a/b"); 
        CompositeName compositeName2 
            = new CompositeName("x/y/a/b"); 
  
        // apply equals() 
        boolean flag 
            = compositeName1.equals( 
                compositeName2); 
  
        // print value 
        if (flag) 
            System.out.println("CompositeName1 is "
                               + "equal to CompositeName2"); 
        else
            System.out.println("CompositeName1 is "
                               + "not equal to CompositeName2"); 
    } 
}
輸出:
CompositeName1 is equal to CompositeName2

程序2:

// Java program to demonstrate 
// CompositeName.equals() method 
  
import javax.naming.CompositeName; 
import javax.naming.InvalidNameException; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InvalidNameException 
    { 
  
        // create Composite name object 
        CompositeName compositeName1 
            = new CompositeName("c/d/a/b"); 
        CompositeName compositeName2 
            = new CompositeName("e/d/a/b"); 
  
        // apply equals() 
        boolean flag 
            = compositeName1.equals( 
                compositeName2); 
  
        // print value 
        if (flag) 
            System.out.println("CompositeName1 is "
                               + "equal to CompositeName2"); 
        else
            System.out.println("CompositeName1 is "
                               + "not equal to CompositeName2"); 
    } 
}
輸出:
CompositeName1 is not equal to CompositeName2

參考文獻:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#equals(java.lang.Object)




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 CompositeName equals() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。