Java Enum compareTo() Method

The compareTo() method of Enum class compares this enum object with the defined object for order.

Enum constants can only be compared to other enum constants of the same type.

Syntax

Parameters

o - It is the enum object that is compared

Return Value

The compareTo() method returns:

  1. negativeinteger, if this enum is less than the defined object.
  2. zero, if this enum is equal to the defined object.
  3. positive integer, if this enum is greater than the defined object.

Example 1

Output:

A2 is greater than A1.

Example 2

Output:

Val1= 2   Val2= 1   Val3= 1
Spring is my Fav season amongst all seasons.

Example 3

Output:

Red is greater than red.