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 Wildcard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wildcard" Watch "Wildcard" New topic
Author

Wildcard

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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)??


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ya you are right Ruben, but we have to give example to make it clear to Duran.



SCJP 6
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Nov 09, 2008
Posts: 598

I dont know why T super is not allowed.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: Nov 09, 2008
Posts: 598

OK,that makes sense.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wildcard