Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics, arrays and wildcards

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
G'day, mates!

Sun provides a tutorial about generics that can be downloaded (70 kb) at
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf#search=%22generics%20gilad%20bracha%22

On page 15 (bottom) of this pdf file you can read:


But in java version 1.5.0 I don't get a warning but a compile error (incompatible types) for this line.

So is this an erratum in the tutorial or is this assignment possible in a newer version of java?


Yours,
AP
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tutorial doesn't say that will work. It says
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alan Moore:
The tutorial doesn't say that will work. It says


I think Amphiprion was referring to the example at the very bottom of the page.

Excellent name choice by the way. Finding Nemo fan by any chance ?
[ August 25, 2006: Message edited by: Joanne Neal ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
generic array creation is not allowed in java and the reason is because it
is not type-safe.

Array store check can't be only performed at compilation time. Check is also required at runtime which you can't do with generics.

e.g.,

Take this code



Error at array creation line is because it is not type safe because you can put String parameterized element in Pair array as below



Line 1 is fine but line 2 should fail but would succeed because of type erasure of generics at runtime. This will defeat the purpose of generics.

- Naseem
[ August 25, 2006: Message edited by: Naseem Khan ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
generic array creation is not allowed in java and the reason is because it
is not type-safe.

Array store check can't be only performed at compilation time. Check is also required at runtime which you can't do with generics.


Not sure what you mean by that. The primary problem with arrays is they are covariant and parameterized types are not. Had Sun listened to folks like Liskov almost a decade ago we'd probably have generic arrays.

Perhaps that's what you're indirectly hinting at. For example, a List[] is by definition an Object[] as well. However, assignment of an object that is not a List should fail and will fail at runtime. Unfortunately when you have a List<String> it is by definition not a List<Object>, but all that can be checked at runtime is that it's a List. In other words, when List<String>[] becomes an Object[] through widening conversion, which is perfectly legal, there's no way of preventing a List<Object> from then being added to the array.



There's other places they could have put the warning. It might have been better for example to put the warning anywhere a generic array is converted to any other type of array.
[ August 25, 2006: Message edited by: Ken Blair ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see how the Pair class will look like after type erasure, thenn everything will be cleared.



After type erasure, Pair class look like



So at runtime, numPairArr[1] = new Pair<String,String>("",""); will also work.

You can add any kind of Pair object in numPairArr.

new Pair(Pair.class, Pair.class) is also fine.

So the concept of generics will fail in the above case.

However, if you go for unbounded wilcard parameterized type, then its fine.




- Naseem
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can add any kind because it's the parameterized type can't be checked. The reason it becomes an issue is because arrays are covariant to begin with.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
Finding Nemo fan by any chance ?

Well, actually I didn't expect my camou being disclosed so fast


And:
Originally posted by Ken Blair:
Yes, you can add any kind because it's the parameterized type can't be checked. The reason it becomes an issue is because arrays are covariant to begin with.


That may be the reason why from these:

Only the first three will produce no warning.
ld produces a compiler error and not only a warning, that was my question in the first place and
le is the only one that does produce a warning.

Yours,
AP
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Burkhard Hassel
That may be the reason why from these:

code:
--------------------------------------------------------------------------------

List[] la = new List [5]; //OK // line 1
List<?>[] lb = new List [5]; //OK // line 2
List<?>[] lc = new List<?>[5]; //OK // line 3
List<String>[] ld = new List<?>[5]; //compiler error? // line 4
List<String>[] le = new List [5]; //Warning // line 5

--------------------------------------------------------------------------------


Only the first three will produce no warning.
ld produces a compiler error and not only a warning, that was my question in the first place and
le is the only one that does produce a warning.


In your latest code, line 4 gives error because a wildtype ? represents a family of all type. So it can't be assigned to concrete parameterized type List<String>[].

Line 5 compiles fine but you will get a warning message because of the assignment of rawType to a concreteType.

Assigning rawList to any concrete parameterized list like IntegerList, FloatList or StringList will always give warning.

Assignment at line 3 will give same warning.




- Naseem
[ August 26, 2006: Message edited by: Naseem Khan ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naseem Khan originally wrote:
In your latest code, line 4 gives error because a wildtype ? represents a family of all type. So it can't be assigned to concrete parameterized type List<String>[].



OK. Thank you all for your responses! My question's been answered. After all perhaps a bottom line is to just avoid generic types in arrays.

Yours,
Bu.


p.m.
One remark unrelated to this thread's subject: I was asked to change my screen name to conform with the naming policy on the ranch.
Since Amphiprion percula (see http://www.fishbase.org ) now is no more, he will turn up again only once in the following snippet:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic