public <T> Collection<T> getCollection(T t, Integer n) {}
what kind of syntaxe is this with 2 return types???
About Question enthuware.ocpjp.v17.2.3099 :
Moderator: admin
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3099 :
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.
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3099 :
i know that but why
public <T> Collection<T> m() {}
and not
public Collection<T> m() {}
why "<T> Collection<T>"
public <T> Collection<T> m() {}
and not
public Collection<T> m() {}
why "<T> Collection<T>"
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v17.2.3099 :
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.
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.
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
Who is online
Users browsing this forum: No registered users and 10 guests