• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

generic question

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this post
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic