T[] toArray( T[] a ); Although I under...">

Confusedly about the following "generics" code

I cannot fully understand the following statement.

<T> T[] toArray( T[] a );

Although I understand that the above statement is a declaration of a function that should be able to receive and return an array of objects of type T ... I do not understand why

1 - Two T not surrounded <>

2 - There seem to be two return types, as in <T> and T[]

+6
source share
3 answers

<T> not a return type. This is a declaration of the type parameter used by this method. Once it is declared, it can be used in the method signature without <> ;

+6
source
  • declares that the method is parameterized by T, in contrast to the containing class, for example. Angle brackets are part of the syntax, not a variable name.

  • As in 1, it is not a return type

+1
source

The return type is T[] , not <T> . That indicates that the method is a generic type, and implementations should accordingly take care of the type.

Two Ts not surrounded by <> are the input parameter type and return type. In this case, both should be the same.

0
source

Source: https://habr.com/ru/post/988921/


All Articles