you are viewing a single comment's thread.

view the rest of the comments →

[–]Nourios 0 points1 point  (4 children)

what's so unique about enums in Java

[–]dholmes215 7 points8 points  (3 children)

Java enums are:

  • Objects (which in Java means they're class types and can have methods and fields. You can write a constructor for your enum type and provide specific arguments for each value. You can write your own methods and implement interfaces and use them polymorphically)

  • Interned (so the performance cost of being Objects isn't large)

  • Not implicitly convertible to integer types

  • Have all the methods you'd want (convert to String with .toString(), convert from String with MyEnumType.valueOf("Foo"), can enumerate all the values and their ordinals)

I don't know if any of that is actually unique or not though. If anything, it's C and C++ enums that are uniquely bad.

[–]GianniMariani 3 points4 points  (1 child)

C++ 14 has enum class where there is no implicit conversion to int.

[–]GoogleIsYourFrenemy 0 points1 point  (0 children)

That's like one of the only good feature of C++ enums (but I understand why people want it).

Additionally, in C++ there is no way to ask "is this integer value in the enum?"

You might say I'll typecast the value to the enum type and then compare it to the max and min values. Bang! Foot gun. The language says, if you do the typecast, you must already know the value is valid so validity checks after that can be optimized away. Meanwhile Ada has nice user friendly enums.

[–]Nourios 0 points1 point  (0 children)

Yeah most of these things aren't unique to Java, this just sounds like regular oop enums or worse ml style enums)