File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Declaring a Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring a Collection" Watch "Declaring a Collection" New topic
Author

Declaring a Collection

Nigel Foley
Greenhorn

Joined: Dec 08, 2012
Posts: 6

Hi All,

I was reading about collections tonight and came across the following code to declare an ArrayList, I have a few questions and I am hoping that someone might be able to help me out





This first example uses the ArrayList class and the second the List interface, what are the pros and cons for each approach?

Any info / links would be greatly appreciated

Thanks
Nigel
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Generally the second one is preferred.
You can use the object according to the List interface, and if you once happen to decide to use a LinkedList instead of the ArrayList, you can switch it without much rewriting.



Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Nigel Foley wrote:Hi All,

I was reading about collections tonight and came across the following code to declare an ArrayList, I have a few questions and I am hoping that someone might be able to help me out





This first example uses the ArrayList class and the second the List interface, what are the pros and cons for each approach?

Any info / links would be greatly appreciated

Thanks
Nigel


Polymorphism is the difference.
Read about it.
the methods that you can call though the ArrayList and List reference can be different.


OCPJP
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7645
    
  19

Ishan Pandya wrote:Polymorphism is the difference.

Actually, it isn't - except in the general sense that any interface method is called polymorphically.

@Nigel: The reason that, as Ivan says, the 2nd method is better is that it doesn't tie your program to a specific implementation. When you're designing something, you don't say "I want an ArrayList of things" (at least you shouldn't), you say "I want a List of things" - and the 2nd declaration does exactly that.

It's called "programming to the interface", and it's a very good habit to get into, especially when dealing with classes of the Java Collections Framework.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Actually, there is a better way still, which is to use generics. Note this particular idiom only works in Java7:-Before Java7, you wrote
Nigel Foley
Greenhorn

Joined: Dec 08, 2012
Posts: 6

Hi all,

Thanks for all the responses and links, its makes much more sense now

Nigel
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
You’re welcome
 
Consider Paul's rocket mass heater.
 
subject: Declaring a Collection