• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there any difference between these two declarations?

 
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
reply
    Bookmark Topic Watch Topic
  • New Topic