• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics confusion

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Can somebody tell me when should I use wildchar character or write Object instead of that in method signature or other places?

e.g
or
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For instance:

 
Kousik Majumder
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the point is that if there is a dependency on other class then we should use wildchar character otherwise not. Is that so?
Can't we use this to make methods generic ?


 
Kousik Majumder
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the point is that if there is a dependency on other class then we should use wildchar character otherwise not. Is that so?
Can't we use this to make methods generic ?

Suppose in an interface what should be the ideal way to declare a generic method?


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules about ?:

- ? super T: the compiler doesn't know a lot about the actual type, only that all instances of T match. Therefore you can add elements that are instances of T or a sub type. When you want to retrieve the compiler knows nothing so Object is the only possibility without casting

- ? extends T: this is just the other way around. The actual type is unknown so adding is not allowed. If T is Serializable, the actual type could be String, Integer, anything. Adding just any Serializable object is therefore not allowed. When you want to retrieve the compiler knows that no matter the type, everything IS-A T, so you can assign the elements to a reference of T without casting.

? is simply the same as "? extends Object".


Now these rules should allow you to choose what you want to return.
- Do you need to put anything in the outer map? Then "?" is not good for the key. The "?"s in the value are still allowed, because any Map is a Map<?,?>. If you don't need to put anything in the outer map then "?" is just fine.

- Do you need to put anything in the inner maps? Then "?" is not good for either the key or the value. If you don't need to put anything in the inner maps then "?" is just fine.
 
reply
    Bookmark Topic Watch Topic
  • New Topic