Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between generic method and wildcard

 
anthony chan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this
 
anthony chan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, thanks Rob
 
Bobby Paulose
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic