CompositeName compareTo() method in Java with Examples

The compareTo() method of a javax.naming.CompositeName class is used to compare this CompositeName with the specified object passed as a parameter. It returns a negative integer, zero, or a positive integer as this CompositeName is less than, equal to, or greater than the given Object as a parameter. If the passed object is null or not an instance of CompositeName then the ClassCastException is thrown by this method.

Syntax:

public int compareTo(Object obj)

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

Return value: This method returns a negative integer, zero, or a positive integer as this Name is less than, equal to, or greater than the given Object.

Exception:
This method throw ClassCastException if obj is not a CompositeName.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// CompositeName.compareTo()
  
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");
        CompositeName compositeName2
            = new CompositeName("x/z");
  
        // apply compareTo()
        int value
            = compositeName1.compareTo(compositeName2);
  
        // print value
        if (value > 0)
            System.out.println("CompositeName1 is "
                               + "greater than CompositeName2");
        else if (value < 0)
            System.out.println("CompositeName1 is "
                               + "smaller than CompositeName2");
        else
            System.out.println("CompositeName1 is "
                               + " equal to CompositeName2");
    }
}

chevron_right


Output:

CompositeName1 is smaller than CompositeName2

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// CompositeName.compareTo() 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("x/y/z/a");
        CompositeName compositeName2
            = new CompositeName("x/y/x/a");
  
        // apply compareTo()
        int value
            = compositeName1.compareTo(compositeName2);
  
        // print value
        if (value > 0)
            System.out.println("CompositeName1 is "
                               + "greater than CompositeName2");
        else if (value < 0)
            System.out.println("CompositeName1 is "
                               + "smaller than CompositeName2");
        else
            System.out.println("CompositeName1 is "
                               + " equal to CompositeName2");
    }
}

chevron_right


Output:

CompositeName1 is greater than CompositeName2

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.