Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Generics and ? symbol

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 74775
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 74775
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic