This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array List Creation Bad Idea

 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Vikash Ananda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Jesper Young,

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