aspose file tools*
The moose likes Java in General and the fly likes Generics and ? symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics and ? symbol" Watch "Generics and ? symbol" New topic
Author

Generics and ? symbol

Jean-Luc Thirion
Greenhorn

Joined: Jan 24, 2008
Posts: 19
Hello,

why has the symbol <?> be added to Java 1.5 (for generics)

Given that scenario (extracted from K&B's book):


But it seems to me that the checkupAll implementation could be replaced by:

In this second implementation of "checkAll" is the List also read-only (probably it is)?

So what is the need for the <?> symbol in the language? I don't totally understand.

Thanks in advance for your help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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.
Jean-Luc Thirion
Greenhorn

Joined: Jan 24, 2008
Posts: 19
So what's the difference between:


and


Is the second declaration not legal ? (I will try it later when i can do it)
Jean-Luc Thirion
Greenhorn

Joined: Jan 24, 2008
Posts: 19
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 !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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.

As I said, maybe somebody else can answer better.
Jean-Luc Thirion
Greenhorn

Joined: Jan 24, 2008
Posts: 19
Ok...
One thing you can't do without wildcards:


So... "List<Dog>� IS-A "List<? extends Serializable>"

Source: today.java.net
[ February 07, 2008: Message edited by: Jean-Luc Thirion ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics and ? symbol
 
Similar Threads
Collections and Generics...help me...
Generics
Help me fix this-Part-2,so I can compile(generics doubt)
Generics
Help me fix this,so I can compile(generics doubt)