File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Difference between generic method and wildcard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between generic method and wildcard" Watch "Difference between generic method and wildcard" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

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: 19685
    
  20

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 - OCEEJBD 6
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: 3617
    
  14

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: 19685
    
  20

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
Bobby Paulose
Greenhorn

Joined: Aug 15, 2013
Posts: 1
Generics are a compile time mechanism.
Wildcards are a runtime mechanism.

consider the following two alternatives
<T> void bar(Blah<T extends BaseType> tb);
void bar(Blah<? extends BaseType> tb);


When you instantiate generics version of bar() with two different types
Blah<DerType> td;
bar(td);
Blah<DerSecondType> td2;
bar(td2);
two implementations of bar() are supposed to be generated by the compiler (see note A below)
void bar(Blah<DerType> )
and
void bar(Blah<DerSecondType> )
Both exist using the mechanism called ‘function overloading’.


However when you use the wildcard version with two different types,
it uses the single implementation for the two calls.
No ‘function overloading’ here.
This is because Blah<? extends BaseType> is supposed to be the base type of both
Blah<DerType> and Blah<DerSecondType>
just as BaseType is the base type of both
DerType and DerSecondType.


note A
Two implementations generated by the compiler is conceptual.
Two implementations are not actually generated by the compiler.
It is optimised into one implementation by the mechanism called 'type erasure'.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between generic method and wildcard