Andre's Blog

20160907 Benefits Of Java Enumerations Compared To C++


The last days I was using java.lang.Enum a lot and I really must say : I AM SO HAPPY ABOUT IT!

I was coding C++ for years and it happens quite often you need the string representation of an enumeration value or you have to find the enumeration value corresponding to a string. In C++ you have to maintain both, the enumeration, which you can use in switch-case-statements and the functions computing one into the other.

You can end up in a worst-case-scenario if you decide to do everything with strings, because then your compiler is not able to find your typos.

In Java you declare your enumeration, e.g.

Enum Colors {

and when you need the string representation you just

String red =;

and when you need the enumeration value you just

String red = "red";
Colors value = Colors.valueOf(red);


Select where to go ...

The Blog
My Technical Blogs
Blogs Of Friends