I am new to java language and have been reading over the API documentations. I was wondering if someone could help me with what some of the symbols mean? For example, in PriorityQueue there is:
Constructor Summary
...
PriorityQueue(Collection<? extends E> c)
...
Method Summary
...
<T> T[]
...
My problem is around ‘?’, ‘E’, ‘c’ and ‘T’. I think I have worked out a few, like ‘T’ I think is Type. If someone could help with understanding I would be much greatful. A link to website that describes would be great! Thank you!
You’re right that
Tis a type parameter. In this case it can be replaced by any type, since it does not have constraints.This constructor has a type constraint:
and should be read as: create a new
PriorityQueueinstance using aCollectiontaking as type parameter any type that extends the typeE(includingEitself), whereEis the type parameter of thePriorityQueue. Example:In this case
E(the type parameter ofpq) isStringand the type parameter oflistmatches the predicate? extends E, since it is alsoString.This would also work:
since
Stringis a subclass ofObject, but this would fail at compilation:I suggest you read more about Java generics here.