Java Collection Framework

A

a

For Collection interface, the declaration of toArray() is:
<T> T[] toArray(T[] a);
Why do we have <T> at the start of the method declaration?
Thanks a lot
 
T

Thomas Weidenfeller

a said:
For Collection interface, the declaration of toArray() is:
<T> T[] toArray(T[] a);
Why do we have <T> at the start of the method declaration?

Because the "genius" that came up with the idea for Java generic decided
that this should be the syntax when you have a parametric (generic)
method and need to provide a type parameter.

And you need to have th <T>, otherwise the compiler would not know that
the two T[] in the method signature refer to a type parameter, instead
of just a 'class T".

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top