| Author |
Difference between generic method and wildcard
|
anthony chan
Greenhorn
Joined: May 21, 2009
Posts: 10
|
|
Hi,
Is anyone able to explain what is the difference between generic method and wildcard like
One difference I know is once you use wildcard you cannot add any element into the collection c but generic method is able to do that. Are there any other differences?
Best regards
anthony chan
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
Have a look at this
|
 |
anthony chan
Greenhorn
Joined: May 21, 2009
Posts: 10
|
|
Thanks,
I have read it, although I am not quite understand about it previously. Can I interpret that generic method is used to express dependencies among the type of one or more arguments to a method and/or return type.
anthony
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
<edit>edited the wrong message</edit>
in the above example, there is not much difference, but I prefer second one.
Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used
consider the below code:
it is more expressive that src may contains subclass of T and you are going to copy the element from src to dest which is a T. here <T> expression depends on <? extends T> . so in this situation you should use generic method instead of wilcard. else I prefere wildcard.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Seetharaman Venkatasamy wrote:
in the above example, there is not much difference, but I prefer second one.
Why do you prefer the second one?
You should avoid using "?" as a type argument, because it is less type-safe than using type arguments, as in the first line.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
It depends. If you don't need T inside the method, then the second form is just fine. It allows you to read the contents without being able to modify the collection. And since T is not needed inside the method, the type obviously does not matter, so using Object for the reference type of the individual elements will be OK.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Jesper de Jong wrote:
Why do you prefer the second one?
even if you use T inside the method, I suppose you can not do useful operations on T . still we can mange with object reference in the case of 2nd.Please, correct me if I am wrong
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
|
Here's a recent discussion that's similar: http://www.coderanch.com/t/540581/java-programmer-SCJP/certification/Generics-method-parameters-wildcards
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Seetharaman Venkatasamy wrote:
Jesper de Jong wrote:
Why do you prefer the second one?
even if you use T inside the method, I suppose you can not do useful operations on T . still we can mange with object reference in the case of 2nd.Please, correct me if I am wrong 
You're not completely right. No, you cannot use T to create instances or arrays. But you can use it as the generic type for other objects. By using a method generic type T you can create an ArrayList<T> inside the method. By using just <?> you cannot. And that's what I meant by needing T inside the method.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Hmm, thanks Rob
|
 |
 |
|
|
subject: Difference between generic method and wildcard
|
|
|