• 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

List question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering why a java programmer using LinkedList or ArrayList would make the pointer be of type List instead of ArrayList or LinkedList. What I mean is, why does a programmer do this:
List <Integer> myList = new LinkedList<Integer>();
intead of this:
LinkedList <Integer> myList = new LinkedList<Integer> ();

I see the first form more but I don't understand why programmers just don't use the second form.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you use the LinkedList declaration. Several weeks later, you find that there's a performance problem, and using an ArrayList instead would be much faster. How much code do you have to change? What if LinkedList<Integer> were the return type of a frequently-called method?

Now suppose you used a List<Integer> declaration instead. How hard would the same change be?

So that's the basic answer. By making the type of a variable, parameter, or return value as general as possible, you isolate design decisions and make code easier to modify.
 
reply
    Bookmark Topic Watch Topic
  • New Topic