public class ArrayHeap<T extends Comparable<T>> implements Heap<T>{
...
public ArrayHeap(int capacity){
heap = (T[]) new Comparable[capacity];
}
Hi there, just have a quick question on the line heap = ... So in class, we have been discussing heaps and implementing them using arrays (using generic types). Just doing some review and can't get my head around what's actually happening here on line 5.
So we cannot create objects of the generic type, normally I would expect to see:
heap = (T[]) new Object[capacity]
I.e. If I was to read this and what it says: Set heap to the result of casting a new Object to a generic array of type T, and set the size to capacity.
My question is, how can the line:
heap = (T[]) new Comparable[capacity];
be read? Is it creating a new Comparable object or a new Object that implements the comparable interface?
new Comparable[N]
creates an array with N
elements which can hold Comparable
or any subtype of Comparable
. No Comparable
is created, just an array.
For the casting, normally if you did something like (String[]) new Comparable[N]
you'll get a ClassCastException
because it's not a valid cast. However, the cast to T[]
is unchecked, which means it doesn't happen at the particular point where the cast expression is. Instead, the cast is erased and somewhere else, outside of your ArrayHeap
class, there's some code like this:
ArrayHeap<String> h = ...;
String s = h.get(...);
and that gets replaced during compilation with something like this:
ArrayHeap h = ...;
String s = (String) h.get(...);
We could therefore say that it's sort of like the (T[])
cast gets moved.
The point, though, is that you don't actually get a T[]
when you do (T[]) new Comparable[N]
. You're just sort of tricking the compiler in to giving you better static type checking inside the body of the ArrayHeap
class. If you tried to return the Comparable[]
to the outside world as a T[]
you'll get an exception.