Collections(Java)-Comparable example program
import java.lang.Comparable;
public class ComparableDemo {
public static void main(String[] args) {
String str1="Comparable";
String Str2="Comparator";
int value=str1.compareTo(Str2);
if(value==0){
System.out.println("Strings are equal");
}
else{
System.out.println("Strings are not equal");
}
Integer obj1=new Integer("37");
Integer obj2=new Integer("38");
int retValue=obj1.compareTo(obj2);
if(retValue>0){
System.out.println("Obj1 is greater than obj2");
}else if(retValue<0){
System.out.println("Obj1 is less than obj2");
}
else{
System.out.println("Obj1 and Obj2 are equal");
}
}
}
Output:
Strings are not equal
Obj1 is less than obj2
Comments
Post a Comment