In both "Enum" and "Enum Class", Enum values can be transformed into integers by using the default method "ordinal()". The "ordinal()" method returns the zero-based position of the enum constant in the list of constants. For example, consider the following enum class:
enum Color {
RED, GREEN, BLUE;
}
To get the integer value of an enum constant, you can call its ordinal() method like this:
Color c = Color.RED;
int value = c.ordinal(); // value will be 0
Note that ordinal values start at 0 for the first constant in the list and increment by 1 for each subsequent constant. So, in this example, RED has an ordinal value of 0, GREEN has an ordinal value of 1, and BLUE has an ordinal value of 2.
Asked: 2022-08-31 11:00:00 +0000
Seen: 1 times
Last updated: Jul 27 '21