Michel Feinstein
Michel Feinstein

Reputation: 14266

Why Android uses Ints instead of Enums

Reading about Android I can see many parts of the framework using int constants for a return value, or configuration value (like in here in the START_REDELIVER_INTENT), instead of an enum, which as far as I know is a better option for many reasons that can be found all around the web, like this.

So this makes me wonder...why Google decided to use so many int's instead of enum's?

Upvotes: 15

Views: 2340

Answers (3)

Eliezer
Eliezer

Reputation: 7347

As some of the other answers correctly state, the reason for this is because enums use more memory than int.

To give context to that decision, you have to remember that the first Android device had 192mb of RAM.

As explained in the closest thing to an official statement from Google, enums aren't just 2x the size of an int, but when the dex is loaded into RAM it could be 13x the size. Therefore the framework team is very careful to prematurely optimize their code in that regard. If there were devices with 4gb of RAM when Android was being developed, maybe this would be different, but we'll never know.

The recommendation for non framework development is to make a judgment for your own use cases. It never hurts to remember that Proguard will usually convert your enums into ints anyways.

Upvotes: 0

Sergey Shustikov
Sergey Shustikov

Reputation: 15821

Operations on int occur many times faster than operations on enum.

Judge for yourself. Each time you create a enum you create as a minimum:

1) Class-loader for it. 
2) You keep this object in memory. 
3) Since enum is static anonymous class - it will always hang in your memory (sometimes even after you close the application.) 

With regard to the Service. In this class, the flags are mainly used for comparisons and return the result to the class above (ContextWrapper). But basically, if you dig into the bowels of Android SDK you will discover for yourself that almost all these flags are used to bynary shift operations.

Even in Java use a binary shift operations in JDK :

/**
 * Max capacity for a HashMap. Must be a power of two >= MINIMUM_CAPACITY.
 */
private static final int MAXIMUM_CAPACITY = 1 << 30;

Also you can look to Window class in Android SDK

/**
 * Set the container for this window.  If not set, the DecorWindow
 * operates as a top-level window; otherwise, it negotiates with the
 * container to display itself appropriately.
 *
 * @param container The desired containing Window.
 */
public void setContainer(Window container) {
    mContainer = container;
    if (container != null) {
        // Embedded screens never have a title.
        mFeatures |= 1<<FEATURE_NO_TITLE;
        mLocalFeatures |= 1<<FEATURE_NO_TITLE;
        container.mHasChildren = true;
    }
}

/** The default features enabled */
@SuppressWarnings({"PointlessBitwiseExpression"})
protected static final int DEFAULT_FEATURES = (1 << FEATURE_OPTIONS_PANEL) |
        (1 << FEATURE_CONTEXT_MENU);

So reasons is two(at least):

  • Less memory consumption.

  • Working faster due bitwise operations.

Upvotes: 6

tyczj
tyczj

Reputation: 73753

pulled straight from the doc's

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

http://developer.android.com/training/articles/memory.html#Overhead

Edit:

also a slide from one of Roman Guy's talks

https://speakerdeck.com/romainguy/android-memories?slide=67

Upvotes: 8

Related Questions