Are there any advantages of using wildcard-type generics in the Bar class over completely skipping them?
public class Foo<T> {}
public interface Bar {
public void addFoo(Foo<?> foo);
public Foo<?> getFoo(String name);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are multiple advantages.
They give more type safety. For example, consider if
FoowasListinstead. If you usedListinstead ofList<?>, you could do this:even if the list was only supposed to contain
Numbers. If you returned aList<?>, then you would not be able to add anything to it (exceptnull) since the type of list is not known.Foois typed on some unknown but specific type.