CompositeName compareTo() method in Java with Examples
Last Updated :
27 Mar, 2020
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:
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
CompositeName compositeName1
= new CompositeName( "x/y" );
CompositeName compositeName2
= new CompositeName( "x/z" );
int value
= compositeName1.compareTo(compositeName2);
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:
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
CompositeName compositeName1
= new CompositeName( "x/y/z/a" );
CompositeName compositeName2
= new CompositeName( "x/y/x/a" );
int value
= compositeName1.compareTo(compositeName2);
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)
Share your thoughts in the comments
Please Login to comment...