Why most examples use an ArrayList

Java development, you have always learned that it is best to create an ArrayList using the List interface as the type of variable in which the list is stored. Like this

List<String> myList = new ArrayList<String>(); 

However, after looking at the many Android examples included in the kit, they created a list using the class.

 ArrayList<String> myList = new ArrayList<String>(); 

Is there a reason why this is done? Is it faster, easier, or something explicitly for setting the class?

+6
java android arraylist
source share
8 answers

In an environment with limited resources, for example, for phones for which Android is designed, it is preferable to avoid using the interface, since this is associated with an additional call of virtual functions.

+9
source share

I would recommend reading Performance Myths , which explains what benefits and problems define a variable like List or ArrayList.

+12
source share

Personally, I believe that the thing that you have always learned does not correspond to the essence. It:

 List<String> myList = new ArrayList<String>(); 

has a very slight advantage in maintenance. If you want to change it to another implementation, there is still only one line left if you use the type of implementation. However, a completely different thing:

 public void process(List<String> list) { 

Here, the use of the interface really matters, because it allows people who call this method to use different implementations without having to change the signature of the method (which they may not be able to).

+10
source share

Although there may be a performance advantage, it is almost certainly tiny and an example of premature optimization.

Most likely, the explanation is that the author of the examples wants to focus on what he (or she) is trying to teach you. Storing a variable and an object with which it refers to the same type is the least to think about. Published code examples are one type of code when you are sure that you will never have to modify it, in which case itโ€™s great to use an ArrayList as a variable type.

+2
source share

When compiling code that works with List <> objects, the compiler must use interface calls, which are more expensive than regular virtual calls, for specific types.

Of course, in the code fragment where the compiler sees an instance of the object and can prove that something declared as List <> is always an ArrayList <>, the compiler should be able to just emit regular calls instead of calling the interface, but methods that are not built-in and work with already created objects, they will not benefit from this optimization.

The ubiquitous optimization that speeds up virtual calls, namely built-in caches (often Polymorphic built-in caching or PIC so as not to be confused with an independent position code), benefits from observing access to instances of only one subclass through a variable of a certain declared type. In this case, after the code has been running for some time, the JIT may optimistically assume that the List <object will ever exist ArrayList <>, create a trap in case the error was incorrect, and fail with using an ArrayList <> call.

Modern processors perform verification very quickly (because they are superscalar and have good branch prediction), so you do not notice the cost of all these virtual call calls and a single implementation interface. But it makes the VM work, tune, generate and fix all this code.

For server software running in a stable state on HotSpot, this does not matter, but for quick launch on a mobile device it may matter - I do not know how good Google VM is.

Good article about this from Dr. Cliff Click, Jr. (conventional large ironing equipment, virtual machine with a hot spot): http://www.azulsystems.com/blog/cliff-click/2010-04-08-inline-caches-and-call-site-optimization

And, of course, "built-in caching" on Wikipedia.

+1
source share

The Interface x = new Implementation() sample is a popular naive attempt to be abstract.

Variable declaration and initialization operator Type x = new Constructor(); , of course, is part of the implementation detail. It is not part of the public API (unless it is public final , but the List is modified so that it is inappropriate)

As an implementation detail, who are we trying to trick this "abstraction" into? It is better to keep the type as specific as possible. Is this a list of arrays or a linked list? Should it be thread safe or not? The choice is important for implementation; we have carefully selected a specific impl list. Then we declare it as a simple list, as if it does not matter, and do we care?

The only legitimate reason to declare it as a List is that I am too lazy to type. It also covers the argument that if I need to go to another list, then I have one more place to change.

This reason is legal only when the scope of the variables is small, and we can see all of its applications at a glance. Otherwise, declare the most specific type so that its performance and semantic characteristics appear in all the code that uses the variable.

+1
source share

There are not many differences, except that they use a much more explicit type in the Android application. Perhaps there are Android methods that rely on getting an ArrayList, not just List.

0
source share

This is the well-known "Design Principle" on how to make a good design, "an interface program, not an implementation." As Michael Borgwardt said, maybe he doesnโ€™t want to use this principle with your local variables, but if you have associations between types, then it makes sense to program interfaces and not implementations to take advantage of OOP. For implementation on interfaces, and not in em, dynamic polymorphic assignment at runtime is allowed. let's say

 interface IAa { say(); } class A implements IAa { public void say() { .. says A ... } } class B implements IAa { public void say() { .. says B ... } } class App { public void someFoo() { IAa myA = new A(); myA.say(); // says A myA = new B(); myA.say(); // says B } ... } 

I don't think that would be otherwise in Android programming :)

0
source share

All Articles