Why are the types different in Java variable declarations? -
this question has answer here:
- arraylist or list declaration in java 10 answers
what purpose of specifying different types on left , right side in java variable declarations?
for example, code standard:
list<string> l = new arraylist<>();
why don't:
arraylist<string> l = new arraylist<>();
or
object l = new arraylist<>(); // silly example shows 2 extremes
- this
list<string> l = new arraylist<>();
allows instantiate types oflist
,arraylist
orlinkedlist
ofstring
. can use methods oflist
arraylist<string> l = new arraylist<>();
can instantiatearraylist
ofstring
.object l = new arraylist<>();
cannot uselist
methods.