+ 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

24th Aug 2021, 2:56 AM
Rishi
Rishi - avatar
2 Respostas
+ 3
Use ordinal method. Example: test.zero.ordinal() returns 0.
24th Aug 2021, 3:11 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 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()); } }
24th Aug 2021, 3:55 AM
ChaoticDawg
ChaoticDawg - avatar