java - Difference of List and ArrayList in declaration? -
this question has answer here:
- type list vs type arraylist in java 14 answers
- list versus arraylist reference type? 3 answers
is there difference between these two? if so, it?
list<integer> x = new arraylist<integer>();
and
arraylist<integer> x = new arraylist<integer>();
the first declaration lets program interface. ensures later on can safely replace arraylist
with, say, linkedlist
, , rest of code going compile.
the second declaration lets program class, potentially use methods of arraylist
not implement list
interface. example, can call ensurecapacity()
on list declared arraylist
, not on list declared list
. although programming interface should preferred, there nothing wrong doing if must call class-specific methods: example, ability call ensurecapacity()
save unnecessary reallocations if know new target size of list.
Comments
Post a Comment