File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic question" Watch "generic question" New topic
Author

generic question

Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 37
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


SCJP 5, SCWCD 5
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jul 24, 2008
Posts: 19
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

Joined: Oct 31, 2007
Posts: 29
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 ]

SJCP 6.0
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 37
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic question
 
Similar Threads
Generics.
Why is not working
Generics Question
Generics and ? symbol
Clarification about ? super Object