• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding creating object

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could you please tell me the difference between creating object like


List l= new ArrayList()

and

ArrayList a= new ArrayList()


Thanks,
Santhosh Kumar V.K
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object created is exactly the same.
The difference is in variable type.
Variable a can only hold a reference to ArrayList (or anything that extends ArrayList -- for example javax.management.AttributeList).
Variable l can hold a reference to anything that implements List interface so you could assign a reference to LinkedList to l.

Generally you should program to interfaces. If your program does not depend on a particular implementation of List (ArrayList in this case), you should define lists as List.
If you ever need to change type of a list, you would need to make only one change in a code (instead of 42381 changes )
reply
    Bookmark Topic Watch Topic
  • New Topic