Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about the following 'generics' code [duplicate]

Tags:

java

generics

I can't seem to fully understand the following statement.

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

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

1 - The two T's are not surrounded with <>

2 - There appear to be 2 return types, as in <T> & T[]

like image 342
Grateful Avatar asked Jun 11 '15 12:06

Grateful


People also ask

What is duplicate code called?

Sequences of duplicate code are sometimes known as code clones or just clones, the automated process of finding duplications in source code is called clone detection.

Is duplicate code a code smell?

Duplicated code is considered one of the worse code smells. Beyond blatant copy paste, there are subtle duplications like parallel inheritance hierarchies and repetitive code structures.

Why is code duplication a problem?

The Issue With Code DuplicationDuplication greatly decreases the maintainability of your code. Ideally, introducing a change in business logic should require your team to change one class or function, and no more. Otherwise, a developer has to spend extra effort hunting down all these extra occurrences.


1 Answers

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

like image 200
Eran Avatar answered Oct 06 '22 15:10

Eran