• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there any difference between these two declarations?  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have instantiated two ArrayList() objects. The first one goes into an ArrayList reference and the second one goes into just List reference. I understand that List is an interface and it cannot be instantiated.. so the following won't work and produces the compiler's wrath.



But is there a preference to using one over the other in any situation or is one more efficient than the other? I might be getting ahead of myself asking this question, but what exactly is happening when you instantiate an ArrayList object and store it in a List reference?

In addition to this, if I could beg one more question here:



I tried to do some research into Object[]. From the Oracle docs "Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class."

My book only lightly graces over it in passing saying that Object[] is more broad than String[] so no casting is required. String[] can go into Object[], but I tried int literal and Integer wrapper class and these produce the scary red lines underneath them. How could I appease the compiler? Again, why would a programmer want to store a String array in an Object reference?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most people prefer:



The whole point of the interface abstraction is that all implementations of the interface should exhibit the same behavior.
By declaring that your variable is of type "List", it decouples your code from the implementation detail that you are using an array list.
If you wanted to change it from an ArrayList to a LinkedList, then you would only have to change this one line of code, and everything else would remain the same.
If you declared the variable as an ArrayList everywhere you used it (especially in method calls), you would have a much harder time of changing it.

Declaring List vs ArrayList (or any other concrete implementation) is not more efficient than the other. However it can save a LOT of time in refactoring/modifying code if you use this pattern as it is intended.


 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be times when you need to reference the implementation rather than the interface, but that would be for special cases for example where access to the implementation-specific methods of ArrayList like ensureCapacity and trimToSize are needed.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We discussed ensure capacity about three years ago. Thing shave changed since then, and it is possible to add a method to an interface, so you can write this sort of thing in the List interface:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!