First, the generation of enum
The syntax of enumerations, including Enum,enumset,enummap, is introduced in Java1.5. Where enum is the enumerated type that we have seen in C or C + +, the enumeration in Java is more sophisticated and complex than the enumeration in C or C + +. In Java, enumerations are a special class, that is, class, so it can do a lot of class-related things, rather than just defining several enumerated values
Second, the official Android recommendations
As as static constants.). Android will assign a single piece of memory to the app when your app is launched. The app's Dex code, heap, and run-time memory allocations are all in this memory.
Like what:
public static final Span style= "color: #0000ff;" >int VALUE1 = public static final 2 ; public static final 3 ;
2 . Use enums to indicate status Public Static enum value{ VALUE1, VALUE2, VALUE3}
Using Enums in 2, theDEX size increase is 13 times times more in case 1. This is just an increase in the Dex code, as well as the memory allocations at runtime, where the declaration of an enum value consumes at least bytes, and the object array that is not counted is required to maintain a reference to the enum value. Using the class file generated in the JAVAP anti-compilation Scenario II, remove the assembly code as follows:
public final class VALUE extends java.lang.enum{ public Span style= "color: #0000ff;" >static final VALUE VALUE1; public static final VALUE VALUE2; public static final VALUE VALUE3; private static final value[] values[]; static {}}
The enum type inherits Java.lang.Enum, and each enumeration item is declared as a static variable and is assigned a value. Value value1 = value. VALUE1 will cause a reference to the static variable.
Iii. How much memory does an Enum in Android account for? How to use it?
Please refer to http://www.liaohuqiu.net/cn/posts/android-enum-memory-usage/
Iv. Alternative programmes
Personal tendencies are associated with interface variables, because the interface automatically sets member variables to static (static), immutable (final), which prevents some cases of incorrectly adding new constants. This also makes the code look simpler and clearer. At the same time, a simple test shows that the same interface (bytecode file) occupies 209 bytes or so, while the class (bytecode file) occupies about 366 bytes of space. Fewer bytecode files mean less cost to load and maintain. In addition, when the JVM loads the interface, it does not need to worry about the extra features provided by the class (such as overloading, dynamic binding of methods, etc.), so loading is faster.
V. Summary
Admittedly, enums makes code easier to read, but avoids enumerations when writing efficient Android code, and we've seen in many classic Java books that enumerations are recommended instead of int constants, but I don't recommend using enumerations in Android development. Especially in large-scale apps, you don't have to use them. because it sacrifices execution speed and dramatically increases file volume. This is also one aspect of performance optimization that reduces oom.
For example: Public class Foo { publicenum shrubbery {GROUND, crawling, hanging}} will produce a 900-byte. class file (foo$ Shubbery. class). When it is first called, the class invokes the initialization method to prepare each enumeration variable. Each enumerated item is declared as a static variable and is assigned a value. These static variables are then placed in a static array variable named "$VALUES" . And so a lot of code, just to use three integers.
Is it recommended to use enum enum in Android?