Open In App

CompositeName compareTo() method in Java with Examples

Last Updated : 27 Mar, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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: Java
// 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");
    }
}
Output:
CompositeName1 is smaller than CompositeName2
Program 2: Java
// 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");
    }
}
Output:
CompositeName1 is greater than CompositeName2
References: https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#compareTo(java.lang.Object)

Article Tags :
Practice Tags :

Similar Reads