Compare To method overview

  • Eclipse: Oxygen
  • Java: 1.8

Compare To method

This is a very useful method in Java. The compared to methods are used to compare TWO OBJECTS and not two primitive datatypes. The output of the compared to the method is explained on the basis of some arguments. If we want to compare two primitive datatypes, we have to use Wrapper Class as studied earlier and convert it to objects.

The following snippet demonstrates the comparison between Part 1 and Part 2.

Let’s understand the compareTo method by the following table.

In the following program comparTo() method compares two Integer object. This method returns the value 0 if both Integer is equal else it only results either negative or positive value.

Output

1

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu