This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic question

 
Mike Mitchell
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers. What would you say is the difference between:

public <T extends Animal> void addAnimal(List<T> animals) { }

and

public void addAnimal(List<? extends Animal> animals) { }

Having trouble sorting out when exactly it's cool to use ?, and when it's not.

Thanks in advance.

Mike
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is almost no difference ... but the first declaration allows you to use generic type T to, for example, declare variable inside the method or something other:

and the second does not allow this.
[ July 24, 2008: Message edited by: Ireneusz Kordal ]
 
Justin Smith
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K&B Chapter 7: Page number 604:

One of the most common mistakes programmers make when creating
generic classes or methods is to use a <?> in the wildcard syntax rather than a type variable <T>, <E>, and so on. This code might look right, but isn�t:

public class NumberHolder<? extends Number> { }

While the question mark works when declaring a reference for a variable,
it does NOT work for generic class and method declarations. This code is not legal:

public class NumberHolder<?> { ? aNum; } // NO!

But if you replace the <?> with a legal identifier, you�re good:

public class NumberHolder<T> { T aNum; } // Yes


Hope this clarfies your doubt !!!
 
doug rosenberg
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin we are talking about generic methods and not generic classes.
I originally thought they were basically the same but I came up some code which allows you to add to the method paramterized with the T.
It is not possible to add to the other methods List.



class Animal{
public String toString(){
return ddd;
}
String ddd = "howdy";
<T extends Animal> void addAnimal(List<T> animals) {
T myAnimal=(T)new Animal();
animals.add(myAnimal);
}}

[ July 24, 2008: Message edited by: doug rosenberg ]
[ July 24, 2008: Message edited by: doug rosenberg ]
 
Mike Mitchell
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I think Doug's identified a key difference: with the ?, an attempt to add won't compile. With the T and a cast, we get a compiler warning, but we're able to get away with the add. Maybe the conclusion is it's better form to use the wildcard.

 
sarvari Tadimalla
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I didnt understand this concept

From K&B Chapter 7: Page number 604:

One of the most common mistakes programmers make when creating
generic classes or methods is to use a <?> in the wildcard syntax rather than a type variable <T>, <E>, and so on. This code might look right, but isn�t:

public class NumberHolder<? extends Number> { }

While the question mark works when declaring a reference for a variable,
it does NOT work for generic class and method declarations. This code is not legal:

public class NumberHolder<?> { ? aNum; } // NO!

But if you replace the <?> with a legal identifier, you�re good:

public class NumberHolder<T> { T aNum; } // Yes

Especially this line "While the question mark works when declaring a reference for a variable,
it does NOT work for generic class and method declarations."....Please expalin with example..

THnaks in advance
 
Rico Felix
Ranch Hand
Posts: 407
5
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this post
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic