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
Joined: Apr 25, 2005
Posts: 809
posted
0
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
Joined: Jul 15, 2003
Posts: 1078
posted
0
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
Joined: Aug 25, 2006
Posts: 1274
posted
0
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
Joined: Apr 25, 2005
Posts: 809
posted
0
Originally posted by Burkhard Hassel That may be the reason why from these:
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.
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:
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.