I am new to java and when I go through the code of many examples on net I see people declaring the variable for an ArrayList
as simply List
in almost all the cases.
List<String> myList = new ArrayList<String>();
I don't understand if there is some specific advantage of doing this. Why can't it be an ArrayList
itself, like so:
ArrayList<String> myList = new ArrayList<String>();
It's called programming to an interface. It allows you to substitute that ArrayList
for a LinkedList
if somewhere down the line you decide that a LinkedList
would be more appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With