(Not So) Stupid Questions 16: What's the Difference Between Wildcard Generics and no Generics?
02/20/2007
Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...." If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.
Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to
This may be a stupid question, but ... "What is the difference between specifying a wildcard for a genericized type and not using the generic notation at all?"
First thoughts:
Take this for example:
interface Example {
public List<?> getList();
}
class ExampleImpl implements Example {
public List getList() { return new ArrayList(); } // warning: needs unchecked conversion to conform to List<?>
}
ExampleImpl successfully implements Example, but the compiler warns me about the conversion of List to List<?>. I don't see how it's even possible to return a List from the method in ExampleImpl that is not conformable List<?>--if that's truly the case, why does the compiler even warn me at all?
It makes me wonder what the benefit is for specifying wildcard generic types for an API. If I were to rewrite the interface's method to return a plain old List, then I wouldn't receive any warnings.
So I don't get it:
What's the point of specifying a wildcard for a genericized type, when you can just omit the generics altogether?
Eve Online ISK (The Inter Stellar Currency) is the currency in Eve. The value of EVE Online ISK goes up and down according to the demand and supply rules. EVE Online ISK can be earned by usual mining, running trade route and manufacturing ships. Some optional ways to earn EVE Online ISK are guarding miner or trade runner in deep space or hunting down pirates and claiming the bounty. Others prefer the opposite, being the pirates themselves and earn more lucrative income. Players spend eve isk to get
Chinese antique furniture has a long history. Especially in the late Ming and early Qing dynasties, it had reached a high level.and we car supplycar wash equipment
The cabinets of Chinese antique furniture have many kinds of style. For the big cabinet can be used as wardrobe. When you open the door, you can put your cloth in it. When you close it, it is an art. The middle size cabinet, it has many shelves and drawers, you can put it in your kitchen as a sideboard, especial, it is easy to clean. Small cabinet is very suit for the space of your bedside with good looking.
The people has begin to realize the value of old furniture. From antique cabinets, we can mostly find the content of Chinese antique furniture and history
You would not BELIEVE how helpful it was for me to consciously DECIDE to ask questions with a girl tonight. It helped TREMENDOUSLY. I learned so much. It sounds so fucking simple but I talked SO much usually that I didnt get the info I needed so I could use that info back to her. mysteryseduceamogflirtpickup
?? @SuppressWarnings works in Jdk1.5 from update 6 onwards. Read the change logs! The reason that List is still allowed without enforcing List<?> is for backwards compatibility. The implementation of generics was designed to work from both sides. I.e. library code compiled with generics can be called safely from legacy code, and conversely so that older non-generified libraries can be called from generic code. If List wasn't allowed then older code would be harder to integrate, though not impossible. When a wild card generic type is specified (i.e. any type with a ? parameter) then you can't call methods taking a generic parameter on that object. For the collection classes this means that you can't call add( T o) on a List<?> for instance.
Be aware that this doesn't add ANY kind of protection against modification though, beyond direct invocation from that reference. It can easily be circumvented by simply casting the object to a List or to List<Whatever> and calling add on that. To stop something being modified, Collections.unmodifiableXXX must still be used.
No real difference
2007-02-21 14:05:36 firefight
[Reply | View]
No benefit
2007-02-20 13:14:06 jezuch
[Reply | View]
There's no "benefit". If the type is generic, you *must* specify type parameters (even if the compiler flags their absence as a mere warning). List<?> basically means "list of anything" or "I don't care about the type parameter, but I have to declare it", depending on context.
No benefit
2007-05-25 17:44:37 dk_says_hey
[Reply | View]
Using no generics, the compiler assumes List<? extends Object>. You can, however, have something else like List<Object[]>.
The difference
2007-02-20 05:11:58 alexsoto
[Reply | View]
The difference is that when you use List<?> as a method parameter you cannot add any object (you can add if you declere as a super type) on them, only can be used for iterating.
But if you declere as List, because no generic is used, you can add new objects.
The difference
2007-02-20 09:23:22 joewolf
[Reply | View]
Interesting, I never noticed that subtlety before. But that still doesn't justify, in my opinion, why the compiler has to emit a warning for an implemention that returns a plain List and not for one that returns a List<String>. In both cases, I can add objects to the returned List if I reference the implementation class directly.
The difference
2007-12-07 05:13:29 jufalcon
[Reply | View]
The difference
2007-02-21 12:47:51 firefight
[Reply | View]
When I compile your code I don't get any warning, it compiles fine without warnings (build 1.6.0-b105). Also that SupressWarnings isn't implemented in jdk1.5, it will compile but does nothing.
The difference
2007-02-21 10:09:27 firefight
[Reply | View]
You can use this method annotation to supress the warning (jdk1.6)
@SuppressWarnings("unchecked")
The difference
2007-02-21 10:28:35 joewolf
[Reply | View]
Obviously...it works in jdk1.5, too...but that still doesn't justify why the warning was emitted to begin with.
The difference
2007-07-15 16:24:16 3ufblog
[Reply | View]
The difference
2007-02-22 21:36:56 deepak_a
[Reply | View]
List<?> is a reference type that can refer to a variety of generic instantiations of List, i.e. you can assign a List<String> or a List<Integer> to List<?> reference. Say, when you are implementing a method that takes any generic instantiation of list, then the method should have List<?> as the parameter.
And, It is not something which should be used for backward compatibility. For more info read blog