# CompareTo method overview

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



### Integer:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LanpOGdgv3_J537-CNX%2F-LanreTibvnuRZHNa-a1%2Fassets_-LTkdx_CHE-ubFCrKQ7__-L_qKfbKBZr39uHGV1M0_-L_qKuiAVB5c2GT17G9k_image.png?alt=media\&token=65edfae3-5818-4d24-a576-1b1fbf681354)

```java
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);
		
	}
}
```

```java
num1 is less than num2 bcoz diff is -ve -1
num3 is greater than num4 bcoz diff is +ve 1
```

### Double :

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LanpOGdgv3_J537-CNX%2F-LanrhfqlBjthYLviDLI%2Fassets_-LTkdx_CHE-ubFCrKQ7__-L_qMEixrbunr9S95AUF_-L_qN-wpjqNg4n_KDhUH_image.png?alt=media\&token=9533d98e-285e-433f-9312-1a416db634e0)

```java
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);
	}

}
```

```java
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:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LanpOGdgv3_J537-CNX%2F-LanrpTrX2EcECFlQY6y%2Fassets_-LTkdx_CHE-ubFCrKQ7__-L_qIkaau7WgLcHAeBB2_-L_qJ17a1M8_NCDpoiqO_image.png?alt=media\&token=8149bd8f-7e6c-4c15-8959-5015ca39d4ed)

```java
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);
		
	}
}
```

```java
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
```

