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
?
This is typically used to replace enums. Google discourages the use of enums on Android app development as stated on their developer guidelines: "Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android."
Adding a single ENUM will increase the size (13x times than the Integer constant) of final DEX file. It also generates the problem of runtime overhead and your app will required more space. So overuse of ENUM in Android would increase DEX size and increase runtime memory allocation size.
An enum in Java is a data type that contains a fixed set of constants. It's a special data type that enables a variable to be a set full of predefined constants. Its variable must be equal to one of the values that have been predefined for it.
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
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);
Less memory consumption.
Working faster due bitwise operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With