Before looking at the code, you have to understand the difference between a Interface and an Implementation. From Wikipedia:
An interface in the Java programming language is an abstract type that
is used to specify an interface (in the generic sense of the term)
that classes must implement. Interfaces are declared using the
interface keyword, and may only contain method signature and constant
declarations (variable declarations that are declared to be both
static and final). All methods of an Interface do not contain
implementation (method bodies) as of all versions below JAVA 8.
Starting with Java 8 default and static methods may have
implementation in the interface definition.1
Once you understand the difference between the implementation and the interface, the two declarations become easier to understand. Let's start with the second statement first - it is more straightforward:
ArrayList<Integer>l=new ArrayList<Integer>();
Here you are declaring l
as being an ArrayList
that accepts Integer
objects. So an eventual use would be something like:
l.add( new Integer(1) );
If you follow that logic then you can see that the other line is the same construct:
ArrayList<List<Integer>>L=new ArrayList<List<Integer>>();
Here you are defining L
to be an ArrayList
that accepts List<Integer>
objects. A List
is an interface in Java - there can be different types of implementations. Ex: LinkedList, ArrayList, etc. The actual implementation is irrelevant in the declaration. So given that an ArrayList is actually a List, you can see that L
can contain instances of l
since l
is a List<Integer>
.
So an eventual use of the second list would be:
L.add(l);
In fact, if you wanted to take it even further, you can do:
List<Integer> linkedList = new LinkedList<Integer>();
List<Integer> vector = new Vector<Integer>();
And add them all to the same L
list since they all implement the List<Integer>
interface:
L.add(linkedList);
L.add(vector);