Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Usage of wildcard?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using of Wildcards,You can use any type of objects: <?>

But it is said the reason for using generic is type safety.

When wirldcards are useful?
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:By using of Wildcards,You can use any type of objects: <?>

But it is said the reason for using generic is type safety.



Actually no. Wildcards doesn't remove type safety. What it does is remove some knowledge of the type -- which in turn allows you to write code that can handle more generic types.

Type safety is still enforced. And it is enforced by restricting what you can do with the wildcard generic. For example, if your code takes a List<?>, it can handle a List of any generic type, but type safety is still enforced, as you won't be able to add anything (but null) to that List -- at least not via the wildcard generic reference..

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your code takes a List<?>, it can handle a List of any generic type,


when a List uses this: <?>

I can enter any kind of object, Right?
 
Stan Weigh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can if you use an ArrayList at least.

Stan
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or LinkedList. Or the object returned by Arrays.asList. Or any other List implementation. But the generic type does not matter; a List<Object>, List<String>, List<Integer>, List<Moose> - it's all allowed.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use List<Object>, You can send any object.When You use List<?>, You can use any object. When You use list Without generic, You can use any object.

Generic is used because you can't send any object you want.Becsaue of safe type.Still I don't understand why use this List<?>
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess reading the Generics chapter of Kathy Sierra's SCJP guide will help you.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you cannot assign a List<Integer> object to a List<Object> reference:
If that line did not provide an error you could add an Object to objectList, which means that that Object would be added to intList as well. In other words, you'd get an Object in a List<Integer>. List<?> solves that by not allowing anything to be added (other than null which is assignable to any reference).
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When You use List<?>,You can use any object that you want.When You use List<object>, You can use any object you want.

The reason we use generic is restriction of data type.

Then How list<?> is useful?
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It allows you to receive a List and retrieve any kind of Object from it.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May you show me by a complete example how can it be useful?
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Effective Java Second Edition has good example.
You can check Sample chapter (Chapter 5) and read Item 23.
It explains why List<?> is safe and List is not safe.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to remember is that List<?> is only valid as a reference type. You can't create a List<?>. A variable of List<?> means "I can point to any type of List you want - but it will still have to be a specific type".

So the difference between a List<Object> and a List<?> is that the former can hold any object, whereas the latter can hold any object of a specific (but unknown) type. That's why you can't add anything to a List<?> - the compiler can't tell what's safe.

But it doesn't mean it's not useful, because sometimes all you need is a reference that can hold any list. Look at some of the methods in the Collections class. They are mainly general purpose methods for working with any sort of list you want (sorting, searching etc), and they make heavy use of wildcards.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class WildCard {


public static void main(String[] args) {
ArrayList<?> i;


i=new ArrayList<Double>();

i.add(new Double(726));
}
}

It has an error in about add method. How must I use add method here?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare "i" as one of the following:
- ArrayList<Double>
- ArrayList<Number> // Double extends Number
- ArrayList<Object>
- ArrayList<Serializable> // Double implements Serializable
- ArrayList<Comparable<?>> // Double implements Comparable<Double>
- ArrayList<Comparable<Double>>
- ArrayList<Comparable<? extends Number>>
- ArrayList<Comparable<? extends Object>>
- ArrayList<Comparable<? super Double>>

Or any of the above with ArrayList replaced by List.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,When Wildcard is used, I can use any type that I want, May you tell me the reason why I must use types you mention?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because those are the types that Double is compatible with - the interfaces it directly and indirectly implements and the direct and indirect super classes. The Comparable is generic by itself so that too has a few options, but again - things that Double is comparable with.

(Note that the <? super Double> is equivalent to <Double> since Double is final.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic