java – Cannot instantiate the type Queue. Why is this?

java – Cannot instantiate the type Queue. Why is this?

In the Interfaces section of the Java docs:

Interfaces cannot be instantiated—they can only be implemented by
classes or extended by other interfaces.

Then you cant directly instantiate the interface Queue<E>. But, you still can refer to an object that implements the Queue interface by the type of the interface, like:

// As I saw that you are adding Characters to your queue
Queue<Character> inputQ = new PriorityQueue<Character>();

You can chose the adequate implementation to use regarding your requirements, here is a list of all concrete and known implementing Classes from its java docs:

ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque,
ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque,
LinkedBlockingQueue, LinkedList, LinkedTransferQueue,
PriorityBlockingQueue, PriorityQueue, SynchronousQueue

In Java, Queue is an interface, you cant instantiate Queue directly. See the documentation here. Please use something like this:

Queue<String> queue = new LinkedList<String>();

java – Cannot instantiate the type Queue. Why is this?

that is because queue is an interface. check out the oracle specs to fine the a concrete class that can be instantiated. link

Leave a Reply

Your email address will not be published.