my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Array List Creation Bad Idea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array List Creation Bad Idea" Watch "Array List Creation Bad Idea" New topic
Author

Array List Creation Bad Idea

Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 115
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

Joined: Mar 24, 2008
Posts: 3775

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32

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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
Hi Jesper Young,

Thanks for the info and making me correct. Now I actually got the answer.
 
 
subject: Array List Creation Bad Idea
 
Similar Threads
Interface/Implemented class reference
thread safety ?
Programming to Interface
Doubt
local variable Vs class level variable