java - Difference of List and ArrayList in declaration? -


this question has answer here:

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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -