+ 2
Enums in Java
How to deal with enums in Java? When I try to print it, it's printing a string. Then how are the comparisons made? String comparisons? How to print or make comparisons with integer values of the enums just like in C++? https://code.sololearn.com/c6JtrGcK2FGH/?ref=app
2 Answers
+ 3
Use ordinal method.
Example: test.zero.ordinal() returns 0.
+ 2
You can do as CarrieForle has suggested, or if you want want to always get the integer value as a String returned instead of the name of the enum ("0" vs "zero"), then you can override the enum's toString() method.
enum test {
zero,
one,
two;
@Override
public String toString() {
return String.valueOf(this.ordinal());
}
}