CompareTo method overview

"If LHS greater than RHS then +ve else -ve"

Integer:

public class IntegerCompareTo {
	public static void main(String[] args) {
		
		Integer num1 =  new Integer(23);
		Integer num2 =  new Integer(28);
		
		int diff =  num1.compareTo(num2);
		System.out.println("num1 is less than num2 bcoz diff is -ve "+diff);
		
		Integer num3 =  new Integer(230);
		Integer num4 =  new Integer(28);
		
		int diff1 =  num3.compareTo(num4);
		System.out.println("num3 is greater than num4 bcoz diff is +ve "+diff1);
		
	}
}
num1 is less than num2 bcoz diff is -ve -1
num3 is greater than num4 bcoz diff is +ve 1

Double :

public class DoubleCompareTo {
	public static void main(String[] args) {
		
		Double distBwSunAndMoon = new Double(554454.5454);
		Double distBwEarthAndMoon = new Double(2333.909);

		int diff = distBwSunAndMoon.compareTo(distBwEarthAndMoon);
		System.out.println("distance between sun and moon is greater than distance between earth and moon diff is +ve " + diff);

		int diff1 = distBwEarthAndMoon.compareTo(distBwSunAndMoon);
		System.out.println("distance between earth and moon is less than distance between sun and moon diff is -ve " + diff1);
	}

}
distance between sun and moon is greater than distance between earth and moon diff is +ve 1
distance between earth and moon is less than distance between sun and moon diff is -ve -1

String:

public class CompareToExample {

	public static void main(String[] args) {
				
		String myName = "abc";
		String yoursName = "abc";
		
		int diff = myName.compareTo(yoursName);
		System.out.println("Same string then, diff is "+diff);
		
		
		myName = "abc";
		yoursName = "bbc";
		
		diff = myName.compareTo(yoursName);
		System.out.println("First char 'a' of first String is 1 less than first char 'b' of second then, diff is "+diff);
		
		myName = "abc";
		yoursName = "adc";
		
		diff = myName.compareTo(yoursName);
		System.out.println("Second char 'b' of first String is 2 less than second char 'd' of second then, diff is "+diff);
		
		myName = "abc";
		yoursName = "aba";
		
		diff = myName.compareTo(yoursName);
		System.out.println("Third char 'c' of first String is 2 greater than third char 'd' of second then, diff is "+diff);
		

		myName = "abc";
		yoursName = "abadfdd";
		
		diff = myName.compareTo(yoursName);
		System.out.println("Third char 'c' of first String is 2 greater than third char 'd' of second then, diff is "+diff);
		
		myName = "abc";
		yoursName = "abcdefdd";
		
		diff = myName.compareTo(yoursName);
		System.out.println("Both strings have first three characters same 4th char of second string 'd' is 5 greater than base char 'a' then, diff is "+diff);
		
		myName = "rrttvv";
		yoursName = "aass";
		
		diff = myName.compareTo(yoursName);
		System.out.println("compare 'r' and 'a' then, diff is "+diff);
		
	}
}
Same string then, diff is 0
First char 'a' of first String is 1 less than first char 'b' of second then, diff is -1
Second char 'b' of first String is 2 less than second char 'd' of second then, diff is -2
Third char 'c' of first String is 2 greater than third char 'd' of second then, diff is 2
Third char 'c' of first String is 2 greater than third char 'd' of second then, diff is 2
Both strings have first three characters same 4th char of second string 'd' is 5 greater than base char 'a' then, diff is -5
compare 'r' and 'a' then, diff is 17

Last updated