Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wildcard

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is wrong to use the wildcard when declaring collections:
Does that mean that the wildcard can only be used when specifying parameters and return types(for collections)
But that it can be used when declaring other types(that are not collections)??
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no special treatment for collections. The places where you can't use a wildcard are:
1. In the generic type declaration for a generic method or class.
2. In an instantiation expression, something like List<T> = new ArrayList<? extends T>
Those are the two that I am aware of. If I missed something, I am sure someone will correct me.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya you are right Ruben, but we have to give example to make it clear to Duran.


 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So;

T super //Not allowed
Wildcard can only be used when defining method parameters or return types(but not generic type declaration)
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duran Harris wrote:So;

T super //Not allowed
Wildcard can only be used when defining method parameters or return types(but not generic type declaration)


Yes not allowed. Think why not allowed?

Wildcard can only be used in declare references.

like

List<? extends Number> list;//declaring references

Actually in method parameters and return types we declare references.


 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know why T super is not allowed.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We use generic programming to bound the types allowed.

If we say <T extends Number>, we are saying, T is number or T is subtypes of Number. So here T has a upper boundary that is Number, that we know.

But if we say <T super Number>, we would say, T is number or T is supertypes of Number. So this will become like saying T could be any object, As Object is the supertype of Number, that does not limit the type of T. This way T could be any object, as every type in java except primitives is an object. We cannot say T has a upper boundary.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK,that makes sense.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic