I have a static 10K file with a word on each line. I need to create a String[] array with all the words. I have 2 options:
Now, my question is, after all this is done, is accesing a word from the array 1 (notice the final keyword) noticeably faster than a word from the array 2 (no final keyword because we load the words at runtime). Theoretically does it make a difference? And we're talking here about Android specifically, not Java. But I am interested in both cases.
Generic Java:
There are no bytecodes for array initialization in the JVM, so the compiler ends up generating individual assignment statements for each array item, which bloats the code. See here for more information.
Loading the values from a file is the most efficient scenario given the amount of data you have. Whether the array is declared final or not is irrelevant, as the strings themselves are immutable.
Android:
The DVM improves on the JVM by adding instructions for initializing arrays. So you don't have the same code bloat issues.
That said, loading things from a file is likely the most flexible approach. Done properly, you could load values from multiple files if necessary, even across the Internet.
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