• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 23928
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: 13081
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.
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic