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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。