aspose file tools*
The moose likes Beginning Java and the fly likes What is the difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference" Watch "What is the difference" New topic
Author

What is the difference

Krishna prasad ambala
Greenhorn

Joined: Jul 03, 2008
Posts: 11


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



What is the difference between the above 2 code snippets.?
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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


My Blog SCJP 5 SCWCD 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jul 03, 2008
Posts: 11
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Krishna prasad ambala, please don't write all in uppercase; I have edited your post.
Read this FAQ, please.
 
 
subject: What is the difference