Page 1 of 1

About Question enthuware.ocpjp.v17.2.3099 :

Posted: Mon Dec 16, 2024 11:43 pm
by raphaelzintec
public <T> Collection<T> getCollection(T t, Integer n) {}

what kind of syntaxe is this with 2 return types???

Re: About Question enthuware.ocpjp.v17.2.3099 :

Posted: Mon Dec 16, 2024 11:58 pm
by admin
There is only one return type and it is Collection<T>. The <T> at the beginning is a type parameter. It is just telling the compiler that the Collection will be of type T.

Re: About Question enthuware.ocpjp.v17.2.3099 :

Posted: Tue Dec 17, 2024 12:46 am
by raphaelzintec
i know that but why
public <T> Collection<T> m() {}

and not
public Collection<T> m() {}

why "<T> Collection<T>"

Re: About Question enthuware.ocpjp.v17.2.3099 :

Posted: Tue Dec 17, 2024 12:50 am
by admin
That's the syntax of the Java language. That is how the language is designed.
Look at the formal parameter list of this method getCollection(T t ). It takes a parameter of type T. How will the compiler know that T is actually a type parameter and not the name of a real type? That is why <T> is used.

Re: About Question enthuware.ocpjp.v17.2.3099 :

Posted: Tue Dec 17, 2024 1:08 am
by raphaelzintec
thank you