aspose 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


Win a copy of Spring in Action this week in the Spring forum!
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: 38
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: 38
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

Joined: May 08, 2014
Posts: 1
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

Joined: Mar 08, 2014
Posts: 288
    
    4

Have a look at this post
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic question