This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
have you read the Java Tutorial? Look particularly at the two sections about wildcards. It means you want the class to be generic for a particualr type, but haven't specified what yet, as a gross oversimplification.
Joined: Jan 24, 2008
So what's the difference between:
Is the second declaration not legal ? (I will try it later when i can do it)
Joined: Jan 24, 2008
Ok the answer to my previous post is: There is NO DIFFERENCE i.e.: - The compilers checks are the same - The generated bytecode is the same
I compiled this piece of code:
Now 1�) If i decompile the bytecode with jad, I get the exact same code, i.e, there is no difference after type erasure:
That proves there is no difference at runtime (it was quite obvious because of type erasure)
2�) Now a comile time. If I uncomment line #1 the code does not compile anymore, and I get the error:
if I comment line #1 and I uncomment line #3, the compiler prevents me from doing that and I get the following compiler error (which is very similar to the previous one):
If I re-comment the lines marked #1 and #3, and I uncomment the lines marked #2 and #4, the code compiles and runs fine.
As a first conclusion, I don't see a difference between both declarations because the behaviour is similar at both, compile-time and runtime. So I really don't understand the need of the <?> symbol in the language. Please tell me !
Joined: Oct 13, 2005
Not sure, maybe somebody else can answer better.
But what you see at runtime can vary from what is written as code. Remember that erasure means the generics information is lost from the bytecode. I think it means that a class Foo<? extends Bar> can be a subclass of Foo<Bar>, whereas a Foo<T extends Bar> isn't a subclass of Foo<Bar>. That is something you would only notice at compile time; by the time the bytecode is created the difference has vanished.