• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics confusion

 
Kousik Majumder
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance:

 
Kousik Majumder
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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: 239
  • Mark post as helpful
  • send pies
  • 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?


 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic