Nikhil
Nikhil

Reputation: 817

create list of Integers in java - cant use new List<Integer>();

What is wrong with the statement?

    List<Integer> phDigits = new List<Integer>();

Error: Cannot instantiate the type List <Integer>

Yes I know i can use new ArrayList<Integer>() instead - I'm asking why the first statement dosen't work.

EDIT: this was a valid question I had.. too simple for SO standards?

Upvotes: 0

Views: 337

Answers (2)

tckmn
tckmn

Reputation: 59303

List is an interface, which means that other classes can implement it. If a class implements List, you can then write

List<Integer> something = new ClassThatImplementsList<Integer>();

List can be used as a type, since anything that implements List is a List, but you can't say "make a List" in Java since it doesn't know how to. All List does is provide the methods you must implement, but it does not contain the implementations.

Upvotes: 6

peter.petrov
peter.petrov

Reputation: 39477

List is an interface. You cannot instantiate it.
This is the problem. It's not that it doesn't work,
it won't even compile.

Upvotes: 3

Related Questions