Search code examples
androidenumsandroid-support-library

enum vs android @Intdef - which one is better optimized


I know that when comparing constants to enums constants take up less space and can be primitive. I am researching @Intdef annotation in android and can someone tell me if its better storage to use @Intdef vs a enum. Is it recommended now in android to put enum aside and use @intdef moving forward when possible ? can @Intdef do polymorphism, i doubt?

from the android docs regarding memory overhead:

Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.


Solution

  • @Intdef is clearly more efficient, it carries zero weight over just having static final ints, it's all compile time instructions. enums are classes and as mentioned in your link have a foot print. @Intdef gets you the most basic functionality of enums namely value validation and none of the other features of enums such as automatic String conversions.

    Much of the android docs is stale, this could very well be one of them. Back in the early days of android every bit counted but now devices are much more capable. Personally I would choose between those two options depending on what's called for by design and not get too caught up with being efficient. Also the syntax for some of these more advanced annotations doesn't make for clean easy to read code, so not a fan there. However if the situation calls for good old static ints the @Intdef will buy you some protection at the expense of visual clutter.