7 ms·
Java's enums are way more powerful than just using a primitive constants. They're fully fledged classes, so even if you can't extend from another class or have
by drKarl 5y ago
Java's enums are way more powerful than just using a primitive constants. They're fully fledged classes, so even if you can't extend from another class or have another class extend from your enum, you can make your enum implement an interface, which means you can have a family of enums. Your enum can have methods and fields, which is massively useful. And enums are basically singleton, so the cost of creating an instance is only incurred once over the lifetime of your application. In your synthetic benchmark you show that when creating a million enumsets it's two or even three orders of magnitude slower than a bitfield, sure. But typical use cases of EnumSets is something that doesn't change that often, so it's very unlikely that you will ever be calling it 1 million times in a row. That means that benefits overwhelming win over performance cost.