Enums in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 - avatar
2 Answers
+ 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 - avatar