java - What is different between this two arrayList definition? -
this question has reply here:
type list vs type arraylist in java 12 answersi utilize 2 statements arraylist definition:
arraylist<string> = new arraylist<>(); list<string> my2 = new arraylist<>();
which 1 should use?
with sec definition, later replace arraylist constructor type of list, linkedlist or high-performant list, or other kind of list may exist in future. in first definition, tied arraylist implementation forever. cannot alter it, because rest of code trusting in reference beingness explicitly arraylist.
by using interface, on contrary, code relies on contract, of interface, , not in particular implementation (like arraylist). gives powerfulness of changing implementations without affecting users. , alter must foster , plan ahead, because cannot prevent things changing.
java arraylist
No comments:
Post a Comment