Javatpoint Logo
Javatpoint Logo

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

Test it Now

Output:

A2 is greater than A1.

Example 2

Test it Now

Output:

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

Example 3

Test it Now

Output:

Red is greater than red.
Next TopicJava Enum



Help Others, Please Share

facebook twitter pinterest