Answer the question
In order to leave comments, you need to log in
What is the difference between new ArrayList() and new ArrayList()?
Hello.
Actually, what is the difference between and
?
PS In the subject of the question, < > is not displayed. List<T> list = new ArrayList<>();
List<T> list = new ArrayList();
Answer the question
In order to leave comments, you need to log in
The form of notation with angle brackets indicates that we used a generic class (generic or generalization) with typed parameters.
Java 7 introduced a shortened notation called diamond. You can omit the parameter on the right side of the expression.
ArrayList catnamesList = new ArrayList<>();
It's worth it just to google it ;)
In this particular case, in Java 1.7 and older, you won't get any visible difference other than the presence or absence of two compile-time warnings. But, of course, angle brackets are better written. The absence of angle brackets means "first I construct an object that is not parameterized at all, and then I assign it to a variable using unsafe type casting", and the presence of them means "choose the appropriate parameters for the object based on the context." The difference can manifest itself, for example, in the following code:
class NumberList<T extends Number> extends ArrayList<T> { ... }
public static void main(String... args) {
List<String> list = new NumberList();
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question