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 ]
Joined: Apr 25, 2005
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 = 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.
Joined: Jul 15, 2003
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.
Joined: Aug 25, 2006
Originally posted by Joanne Neal: Finding Nemo fan by any chance ?
Well, actually I didn't expect my camou being disclosed so fast
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.
Joined: Apr 25, 2005
Originally posted by Burkhard Hassel That may be the reason why from these:
List la = new List ; //OK // line 1 List<?> lb = new List ; //OK // line 2 List<?> lc = new List<?>; //OK // line 3 List<String> ld = new List<?>; //compiler error? // line 4 List<String> le = new List ; //Warning // line 5
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.
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: