• 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

Array List Creation Bad Idea

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
ArrayList arrList =new ArrayList(); //Bad Idea

List arrList=new ArrayList(); //good idea

I could see the word "good idea and Bad idea" in some of the books and site, but none of them tell why? Could please any tell why first one was Bad idea and second one is good idea.

Regards,
Sankar. S
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using java.util.List is a more generic approach opposed to using ArrayList as you can pass in any implementation of List, not just ArrayList.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't want to lock yourself into a specific data type. by saying "List arrList =", you can later change the "new ArrayList(); " to ANY object type that implements the List interface.

I think this is less of an issue when you are writing your own, small applications. It becomes more of an ordeal when your team is writing part of the code, and another team is writing a different part. They are going to provide you a way to get an object. They should give you an interface name, like "List", so that 6 months, if they decide to completely change things around, you don't have to touch your code.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

The code



In general we must use List arrList=new ArrayList(); and likewise for other similar things because the implementation of class ArrayList can be changed by Sun in newer version of Java hence operations on ArrayList may fail on newer version of JDK but using List arrList=new ArrayList(); the code is safe from upgradation of JDK.

For safety purpose the good idea one is better approach if JDK is upgraded.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vikash Ananda wrote:In general we must use List arrList=new ArrayList(); and likewise for other similar things because the implementation of class ArrayList can be changed by Sun in newer version of Java hence operations on ArrayList may fail on newer version of JDK but using List arrList=new ArrayList(); the code is safe from upgradation of JDK.

For safety purpose the good idea one is better approach if JDK is upgraded.


Sorry Vikash, but your answer is wrong. This is not the reason why you'd want to use List vs. ArrayList on the left side of the =. The implementation of ArrayList is not going to change in a newer version of Java so that "operations on ArrayList may fail". And using List instead of ArrayList is not going to make your code "safe" in any way with regard to a new version of Java.

Fred gave the correct answer: It's because you don't want to lock yourself in to one specific implementation of interface List (in this case, ArrayList).

Suppose that later you find out that it would be better to use another implementation of List, for example LinkedList. If you would have made the variable arrList an ArrayList, this means you would have to search through the whole program, and check everywhere where arrList is used if it's not doing something specific for ArrayList. If you would have written this:

then you would only have to change that line of code to the following:

The rest of the program only knows that arrList is a List, so you're sure that the program doesn't use any ArrayList-specific stuff on arrList.
 
Vikash Ananda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper Young,

Thanks for the info and making me correct. Now I actually got the answer.
reply
    Bookmark Topic Watch Topic
  • New Topic