Open In App

CompositeName compareTo() method in Java with Examples

Last Updated : 27 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
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 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 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads