What is the difference between the following two statements, in initializing an ArrayList
?
ArrayList<String> a = new ArrayList<String>();
ArrayList<String> a = new ArrayList<>();
What is the difference between the following two statements, in initializing an ArrayList
?
ArrayList<String> a = new ArrayList<String>();
ArrayList<String> a = new ArrayList<>();
Before Java 1.7, only this one is permitted:
ArrayList<String> a = new ArrayList<String>();
And in 1.7, this is added, which is the same but shorter: (all programmers are lazy)
ArrayList<String> a = new ArrayList<>();
The latter uses an inferred type introduced in Java 7. The syntax (known as diamond operator) is illegal for Collections before Java 1.7 so the former is used for those earlier versions.
The diamond operator reduces the verbosity of the declaration.
There is no difference. The second option (called Diamond Operator) is a shortcut. The compiler will infer that the type parameter of the generic ArrayList must be String.
Second option was uses a concept that was introduced in java 7 - inferred types. Apart from that and assuming you are using java 7 the effect of the two calls should be the same. On earlier java versions you can not use second version of your code.