This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference

 
Krishna prasad ambala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


List l=new ArrayList();
ArrayList al=new ArrayList();



What is the difference between the above 2 code snippets.?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both ArrayList Objects are same. The first one is accessed using an interface reference while the second one is accessed using ArrayList reference. The difference is that in first declaration of ArrayList you can only call those methods which are defined in List Interface.

ArrayList class implements List Interface so it implements all methods given in List Interface and also has some methods which are specific to ArrayList. Those methods which are specific to ArrayList cannot be called using reference of its Interface(List).


HTH
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also means you can change the implementation and use a LinkedList instead, by saying

List<Foo> fooList = new LinkedList<Foo>();

or keep the ArrayList

List<Foo> fooList = new ArrayList<Foo>();
 
Krishna prasad ambala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends for giving some usefull info abt my code.

Advanced Happy New Year.

[edit]Get rid of UPPERCASE. CR[/edit]
[ December 31, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna prasad ambala, please don't write all in uppercase; I have edited your post.
Read this FAQ, please.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic