What is the difference between
List<String> list = new ArrayList();
and
List<String> list = new ArrayList<>();
?
In the first case, the IDE highlights it and says "Unchecked assignment", but they seem to behave exactly the same.
What is the difference between
List<String> list = new ArrayList();
and
List<String> list = new ArrayList<>();
?
In the first case, the IDE highlights it and says "Unchecked assignment", but they seem to behave exactly the same.
The type of each entry in the ArrayList has not been specified, only the List, hence why the Unchecked assignment
warning. You should explicitly state it in one of two ways...
List<String> list = new ArrayList<String>();
Or, you can abbreviate that now (since Java 7) to...
List<String> list = new ArrayList<>();
...and the compiler will then be able to implicitly pick up the type from the List's type designation.
No difference, java compiler could figure out the type, but better add the inferred type argument:
List<String> list = new ArrayList<String>();