CompositeName equals() method in Java with Examples

The equals() method of a javax.naming.CompositeName class is used to compare this CompositeName with the specified object passed as a parameter and checks whether two objects are equal or not. If both objects are equal then the equals() method returns true else false. If passed obj is null or not a composite name then the method returns false. Two composite objects are equal if each component in one is equal to the corresponding component in the other.

Syntax:

public boolean equals(Object obj)

Parameters: This method accepts obj which is the possibly null object to compare against.

Return value: This method returns true if obj is equal to this composite name, false otherwise.

Below programs illustrate the CompositeName.equals() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

CompositeName1 is equal to CompositeName2

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

CompositeName1 is not equal to CompositeName2

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.