*
The moose likes Beginning Java and the fly likes Usage of wildcard? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Usage of wildcard?" Watch "Usage of wildcard?" New topic
Author

Usage of wildcard?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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

Joined: Jul 20, 2010
Posts: 6
You can if you use an ArrayList at least.

Stan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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

Joined: Jul 29, 2008
Posts: 275

I guess reading the Generics chapter of Kathy Sierra's SCJP guide will help you.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Oct 13, 2005
Posts: 37953
    
  22
It allows you to receive a List and retrieve any kind of Object from it.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
May you show me by a complete example how can it be useful?
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Usage of wildcard?
 
Similar Threads
Best portal implementation
Issue while retrieving BLOB from ORACLE 9i, using Hibernate 3.0
Problem in Displaying Non english content in a UTF-8 JSP
security warning while running servlet
Inserting a blob using hibernate