The method compares the Number object that invoked the method to the
argument. It is possible to compare Byte, Long, Integer, etc.
However, two different types cannot be compared, both the argument and the Number object invoking the method should be of same type.
However, two different types cannot be compared, both the argument and the Number object invoking the method should be of same type.
public class Test { public static void main(String args[]) { String str1 = "Strings are immutable"; String str2 = "Strings are immutable"; String str3 = "Integers are not immutable"; int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); result = str3.compareTo( str1 ); System.out.println(result); } }
output:
0
10
-10
-------------------------------------------------------------------------
public class Test{
public static void main(String args[]){
Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
}
}
output:
1
0
-1
we cannot apply compareTo on primitives.
No comments:
Post a Comment