About Question enthuware.ocpjp.v17.2.3099 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

About Question enthuware.ocpjp.v17.2.3099 :

Post by raphaelzintec »

public <T> Collection<T> getCollection(T t, Integer n) {}

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

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

i know that but why
public <T> Collection<T> m() {}

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

why "<T> Collection<T>"

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

thank you

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests