• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference Between These 2 Declarations.

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurus,

Please explain the difference between these two declarations.

ArrayList myList = new ArrayList();

List myList = new ArrayList();


Regards

Suresh.
 
Thirugnanam Saravanan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh

ArrayList myList = new ArrayList();

This is an ordinary instantiation of the Class ArrayList.
List myList = new ArrayList();

This is where Polymorphism came into picture in java. You should have heard some where that :
A reference variable can refer to any object of the same type as the declared
reference, or�this is the big one�it can refer to any subtype of the
declared type!

Here the List Interface Reference variable (myList) is referring to its subtype (in this case its implementing class ArrayList Type). In addition to ArrayList , it can refer any of its subtype like LinkedList,Vector etc..

Let me know if you need more information on this.
[ February 08, 2008: Message edited by: Thirugnanam Saravanan ]
 
Sheikh Sadiruddin
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell us which declaration is good for programming.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither.

You should say
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I think you needed to explain your post more - this is the Beginner forum.

The original question was about variables that refer to collection objects. Should one declare them as being of the exact type (e.g. ArrayList) or of the implemented interface (e.g. List)? The answer is "it depends".

Declaring as ArrayList is more restrictive. If there is a good reason why only an ArrayList would be suitable, then you should declare the variable as an ArrayList. This also may give very slightly better performance, due to simplified method dispatch, but don't get excited about that.

Declaring as List is more flexible. If there is good reason to think that ArrayList is not the only kind of List that could be used, then it is better to declare the variable as a List. This allows the code to be changed very simply in the future, if another kind of List is deemed suitable.

I think that point that Campbell was making is that, if using Java 5 or better, collections should always be declared with a type ("generics"). If your List is only going to be storing objects of class Foo (or subclass), declare it as List<Foo>. This will make your code more concise, and also allows the compiler to spot certain types of bugs, which it could not spot if you used a plain List.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Peter, you are right. You have explained what I ought to have explained myself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic