This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

wildcard operator - one doubt ...

 
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 .
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want a pre-generics List without compile-time warnings, then you need to specify the type parameter as Object.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rathi ji kya khatru question poochha hain what is ur source of that q.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abe guddu ,
Abhi-abhi bas dimag me aaya aur type kar diya ....
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
greenhorn
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 !!!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahem...

Please use English here at JavaRanch. Thanks.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Jim .
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!