• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused about java 1.5 wildcards  RSS feed

 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm confused about why we need a different syntax for ? (wildcards). if i have a method



that iterates through the list and prints out every object in the ArrayList then this code



should be able to achieve the same effect.

tia.
 
Paul Clapham
Sheriff
Posts: 22506
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct that those two methods would have the same effect. The difference is that for the first, you call it like this:where anyArrayList is declared as ArrayList<Whatever>, and for the second you have to call it likei.e. you have to pass a type parameter to the method. Since this is redundant, it's a bad thing. That's why the first version exists.
 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it seems that you dont have to bound the generic type when calling a static method, so the syntax to call both methods should be the same.
tia.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and for the second you have to call it like

i.e. you have to pass a type parameter to the method. Since this is redundant, it's a bad thing. That's why the first version exists.
;

Are you sure about that, I'm almost certain that thats invalid syntax in any context. You could call the method as


As a matter of fact, the program will not compile if you try to overload the mehtod with both versions of the syntax. The compiler will 'complain' with the message:

reference to printTest is ambiguous, both method printTest(java.util.ArrayList<?> ;) in MethodCheck and
method <E>printTest(java.util.ArrayList<E> ;) in MethodCheck match
printTest(al);

[ May 07, 2006: Message edited by: Garrett Rowe ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would be able to "print" because the toString method is available on the type Object (not that this is a good thing). If you wished to call any method on a non-Object reference type, you'd need to cast (ick!) or you'd use the other version of your method (better).
 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think a good way to disambiguate between wildcards ('?') and just generic type ('E') is to present a situation where the use of wildcards is the only choice. I've tried to look around but can't find that situation. am still confused.

tia.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you have a local variable

List<? extends String> stringList;

How do you express this without wildcards?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Say you have a local variable

List<? extends String> stringList;

How do you express this without wildcards?


Why can't you just call it List<String> if is a local variable and not a method parameter?
[ May 07, 2006: Message edited by: Garrett Rowe ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:
Why can't you just call it List<String> if is a local variable and not a method parameter?


OK, not the best example, as String is final. In general you would do it for the same reasons you declare the variable as a List instead of an ArrayList, even if you know that it's currently an ArrayList - for reasons of flexibility.

The following might be a more compelling example, though:

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make sure that one earlier issue is clear: Paul Clapham was mistaken, and Garrett Rowe was correct. The two syntaxes are identical in effect, including the way you call the method.

Both <E> and <?> represent an unknown/variable type. The difference is, <E> gives the unknown type a name, and <?> does not. Why give the type a name? Generally, only if you need to refer to that type in more than one place. A name provide a link between two or more unknown types, indicating that while the type is unknown, it is the same type being used at those two or more locations in the code. If you need to refer to the same type in two or more places, you need to give the type a name. If you only need it in one place, you can use a name, but it's probably shorter and simpler to skip that and use <?> instead.

Moving to later in this thread... Ilja correctly observes that for a local variable, you can't introduce a named type variable; only the <?> syntax is allowed. To me, this then raises the question of why is there no syntax to allow naming a local type variable? It seems it hasn't been necessary - but what's not clear is why hasn't it been necessary? I can't think of any specific things that one would need a local named type variable for. And ultimately that may be all the reason we need. Unless and until someone discovers a reason we need named local type variables, there's no point to creating a syntax to allow them. But I have a feeling there's a more fundamental reason why they're not needed, while anonymous local types (<?>) are needed. I just can't quite nail it down.
[ May 07, 2006: Message edited by: Jim Yingst ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

I see now, I just didn't understand the example at first. I was thinking of adding items to the list not assigning a value to the list.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!