• 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

advantage of declaring list

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

1) ArrayList myList = new ArrayList();

2) List myList = new ArrayList();

we can declare an arrayList as these two ways .
1) what are the benifit of these two ways.
2) is this both ways are same or not?
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To achieve polymorphism. Your myList won't be tied to the implementation. If suppose you want to change the implementation of ArrayList() to something else, you can do it easily using the first approach.

 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In writing this way, if you later decide that instead of ArrayList, LinkedList would perform better in the application then you just have to change a single line of code and everything would still work correctly.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Ali Khojaye wrote:In writing this way, if you later decide that instead of ArrayList, LinkedList would perform better in the application then you just have to change a single line of code and everything would still work correctly.



Also... if you want to use it in threaded fashion, you can "change a single line of code" to get it from Collections.synchronizedList(). Of course, in that case, you have to use the List interface, as the class files for the sychronization list wrappers are not in scope, out of the Collections class.

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is just my PERSONAL opinion, but...

When you're writing all the code yourself, and writing rather simple programs, I don't see the advantage. The real power comes in when someone else uses your code. You provide them a method they can call to get an object they want to use. if you tell them to expect an ArrayList, then you are locked into always using an ArrayList.

However, if you tell them to expect a List, you can change your code to return anything, any time you want, as long as it's still a List.
 
reply
    Bookmark Topic Watch Topic
  • New Topic