Should we refer to objects by their interfaces in the Android platform?

I use the advice suggested by Joshua Bloch Effective Java, Item 52: Refer to objects by their interfaces .

However, most code examples have Android, I understand that the following code is pretty common.

 private ArrayList<Integer> mPhotos = new ArrayList<Integer>(); 

I understand that this is due to optimizing performance, as the following code will be slower.

 private List<Integer> mPhotos = new ArrayList<Integer>(); 

However, is there such an optimization technique? As if I read from http://developer.android.com/guide/practices/design/performance.html

On devices without JIT, it is true that calling methods using a variable with the exact type rather than an interface is somewhat more efficient. (So, for example, it was cheaper to call methods on a HashMap map than a Map, although in both cases the map was a HashMap.) It wasn’t that it was 2 times slower; the actual difference was more like 6% slower. In addition, JIT makes two virtually indistinguishable.

Should we assume that our devices do not have JIT, and refer to objects without interfaces? Or, will we just take the advice of Joshua Bloch?

+6
source share
2 answers

Starting with Android 2.2, Dalvik VM (which runs the Dalvik bytecode that is the result of your Java source code) has a Just-in-time (JIT) compiler.

I don't know if this particular optimization is part of JIT or not, but it needs to be tested on real devices.

If you target devices to 2.2 and this 6% overhead (which should not be confused with a 6% slowdown in your application!), This will seriously affect your application, then this optimization may be useful.

+2
source
 private ArrayList<Integer> mPhotos = new ArrayList<Integer>(); 

This is preferable to a performance reason. This is a private variable using the most famous type.

-3
source

Source: https://habr.com/ru/post/892236/


All Articles