Reputation: 34750
I have a generic Parameter interface and its ParameterImpl concrete class. The T will be a Float, Long, Integer or String.
public interface Parameter<T extends Comparable<T>> {}
public class ParameterImpl<T extends Comparable<T>> implements Parameter<T> {}
I have several other ordinary classes which will use above generic Parameter type with warnings, like the one followed:
public class ProcessParameter() {
Map<String, Parameter> params; //use <?> or @SuppressWarnings ???
//use <?> or @SuppressWarnings or <T extends Comparable> ???
public Parameter getParameter(String key, Map<String, Parameter> paramMap)
{
}
//use <?> or @SuppressWarnings or <T extends Comparable> ???
public void addParameter(Parameter param)
{
}
//use <?> or @SuppressWarnings or <T extends Comparable> ???
public Map<String, Parameter> getParameterMap()
{
}
//many more cases with Parameter presents
}
For the Map<String, Parameter> params;
variable case, should I use <?>
or @SuppressWarnings
? Why you suggested way is better?
For the method cases, if I use <?>
or <T extends Comparable>
(in the method signature), then it will affect all other methods related. The @SuppressWarnings
will cover this issue just here. So, which option I should choose? Why the option you suggested is better than others?
In most of cases, the Eclipse suggests the @SuppressWarnings
way. Is the @SuppressWarnings
a right way or it is a temporary solution with potential bug inside?
Upvotes: 11
Views: 15484
Reputation: 88707
I would use <?>
if the parameter types might be different. Using raw types and thus @SuppressWarnings might cause the compiler to disable generic type checks further down the line (depends on how you use those parameters) and that's probably not what you want.
Of course, you can't alter the value of an instance of Parameter<?>
but that's probably not wise anyways, since you don't know the type anymore.
For more information on raw types, have a look here: What is a raw type and why shouldn't we use it?
Upvotes: 1