Forums Register Login

Generics confusion

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
For instance:

+Pie Number of slices to send: Send
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 ?


+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
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.
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 978 times.
Similar Threads
how can i deal with this unchecked warning?
Problem while using Map in ActionForm
Using toArray(Object[]) in java 5
non-static reference in singleton
problem to get values in hasmap in bean:write
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:39:06.