aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wildcard operator - one doubt ... 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 "wildcard operator - one doubt ..." Watch "wildcard operator - one doubt ..." New topic
Author

wildcard operator - one doubt ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


I found this from one tutorial , Is this the only use of wildcard operator ... [ when you don't want any specific paramaterized list , or can say list like 1.4's list ]

Thanks a lot .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
when you don't want any specific paramaterized list , or can say list like 1.4's list

There is a difference between List<?> and List.

For example,

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you really want a pre-generics List without compile-time warnings, then you need to specify the type parameter as Object.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Joyce: I've never seen (nor can I imagine) the wildcard type parameter used outside of generic method arguments. Are there other valid uses for the wildcard?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Marc,

Some examples of wildcard type parameter used on non-generic method can be seen in Collection interface like addAll, containsAll, removeAll and retainAll and its subclasses.

If you really want a pre-generics List without compile-time warnings, then you need to specify the type parameter as Object.

Are you referring to something like this?



Joyce
[ March 03, 2005: Message edited by: Joyce Lee ]
Guddu Jhakas
Greenhorn

Joined: Feb 20, 2005
Posts: 9
rathi ji kya khatru question poochha hain what is ur source of that q.


God is inside u<br />Belive him
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Abe guddu ,
Abhi-abhi bas dimag me aaya aur type kar diya ....
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Joyce Lee:
... Are you referring to something like this? ...

Exactly. If a person really wanted to do this, specifying the type parameter as Object would "simulate" a pre-generics collection: No type-safety and no implicit casting.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Joyce Lee:
Some examples of wildcard type parameter used on non-generic method can be seen in Collection interface like addAll, containsAll, removeAll and retainAll and its subclasses.

Thanks for the response!

First let me verify some terminology: When you say "non-generic method," you're talking about a method that does not specify its own type parameter, but still might take arguments of a generic type. For example, public void meth1(List<Integer> inList){} would be non-generic, as opposed to the obviously generic, public <T> void meth2(T t){}. Correct?

My impression from reading Schildt (referenced here) is that both would be considered "generic methods." If this isn't accurate, then the way I phrased my question would have been misleading.

What I'm really asking is this: Are there any valid uses of the wildcard outside of a method's argument list? For example, would it ever make sense to do the following (which, as you point out above, opens the door for some interesting compilation errors)?

List<?> myList = new ArrayList<Integer>();
[ March 03, 2005: Message edited by: marc weber ]
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213

rathi ji kya khatru question poochha hain what is ur source of that q.



Abe guddu ,
Abhi-abhi bas dimag me aaya aur type kar diya ....


jyada masti mat karo bacho
varna firangi naraj ho jayenge !!!


:d
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ahem...

Please use English here at JavaRanch. Thanks.


"I'm not back." - Bill Harding, Twister
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
For example, public void meth1(List<Integer> inList){} would be non-generic, as opposed to the obviously generic, public <T> void meth2(T t){}. Correct?

Yeah, that's the impression I gathered from the generics tutorial chap 5. Please feel free to comment if you guys think otherwise.

List<?> myList = new ArrayList<Integer>();

For the above statement, I couldn't think of a reason of using it since no element can be added to myList afterwards. I just used it to illustrate the constraint of List<?>. How about using it as a return type?

For example:
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

...that's the impression I gathered from the generics tutorial chap 5. Please feel free to comment if you guys think otherwise...

To me, it makes sense that a "generic method" needs to specify its own type parameter, since this parallels the idea of a generic class. But no one asked me. Other material I've read (in particular, Herbert Schildt's Java 2 v5.0 (TIGER) New Features) seems to indicate that any method taking a parameterized argument is a "generic method." Perhaps I'm misinterpreting. I don't really know...

... How about using it as a return type? ...

That also works in the sense that it compiles and runs. But we still end up with a variable that (apparently) can't be dereferenced, so I'm still questioning whether this is a valid use.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Sorry Jim .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wildcard operator - one doubt ...