I am new to Java. I want to know the difference between:
List< String > list = new ArrayList<>();
and
ArrayList< String > list = new ArrayList<String>();
and
ArrayList< String > list = new ArrayList<>();
Thanks
The first one is only valid since Java 7, and is the equivalent of
List<String> list = new ArrayList<String>();
It's just less verbose.
Same for the third one, which is equivalent to
ArrayList<String> list = new ArrayList<String>();
and thus strictly equivalent to the second one.
You should prefer the first one, for the reasons mentioned in the answers to the following question: List versus ArrayList as reference type?
The three are somewhat equivalent:
List<String> list = new ArrayList<>();
In the above, you're declaring a variable that implements the List
interface which will contain String
elements, and instantiate it with the concrete class ArrayList
. Also, you're using Java 7's new diamond syntax, son you don't have to write again String
between the <>
.
ArrayList<String> list = new ArrayList<String>();
In the above, you're declaring a variable of the concrete class ArrayList
which will contain String
elements, and instantiate it with the concrete class ArrayList
using the "traditional" syntax which mandates that you specify the String
type between the <>
.
ArrayList<String> list = new ArrayList<>();
In the above, you're declaring a variable of the concrete class ArrayList
which will contain String
elements, and instantiate it with the concrete class ArrayList
. Also, you're using Java 7's new diamond syntax, son you don't have to write again String
between the <>
.
Be aware that the diamond syntax (<>
) will only work in Java 7 and above, for previous versions of Java you're stuck with using the traditional syntax (<the-type>
) for instantiating generics.
The last two forms are completely equivalent; the first form is a bit different since you're specifying that the list
variable is of type List
and not of type ArrayList
- and that's the preferred form, since good object-oriented practices dictate that you should program to an 'interface', not an 'implementation'.
There are two points to remark here:
1.The new Java 7 diamond operator allows you to instantiate a generic class without specifying the type parameter on both sides. So these two are equivalent:
ArrayList< String > list = new ArrayList<String>(); ArrayList< String > list = new ArrayList<>();
2.The more important point is the difference between the first two instantiations. The second one is clear:
ArrayList< String > list = new ArrayList<String>();
In the first one:
List< String > list = new ArrayList<>();
you are using the fact that ArrayList
is a subtype of List
and therefore the assignment is valid. However, on the new list
object you only have the subset of methods that are included in List
(because your object is declared as List
after all), but with the implementations present in ArrayList
. This is called polymorphism in object-oriented programming and allows you to use a subtype of a class instead of the parent, where the parent is expected, in order to provide various different functionalities.