# CompareTo method overview

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

### Integer:

![](/files/-LanreTibvnuRZHNa-a1)

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

![](/files/-LanrhfqlBjthYLviDLI)

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

![](/files/-LanrpTrX2EcECFlQY6y)

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-core-java-for-java.gitbook.io/project/compareto-method-overview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
