List<integer> c1=new Arraylist<integer>what type object is c1

In collections in java

List<Integer>  c1=new Arraylist<Integer> 

Here, c1 is an object of type List which is an interface, and we can't create an object of that interface?

Can somebody explain this to me?

Jon Skeet
people
quotationmark

The type of the variable c1 is List<Integer>. That just means that the value of c1 at any time has to either be null, or a reference to an object whose type implements List.

However, the type of the object that the value of c1 refers to at execution time is ArrayList. (It doesn't know it was constructed as an ArrayList<Integer> due to type erasure.)

It's very important to distinguish between three concepts:

  • Variables (such as c1). A variable has a type at compile time.
  • References (such as the value of c1)
  • Objects (such as the one the value of c1 refers to). An object has a type at execution time (it doesn't exist before then).

If you can keep those three straight in your head, it makes a lot of things (like parameter passing, assignments etc) a lot simpler.

people

See more on this question at Stackoverflow