Howdy ranchers!
The problem is that you cannot really make an array of generic types.
So you can not say:
Gen<String> arr[] = new Gen
<String>[5]; // doesn't compile.
Therefore in your code (that compiles with warnings) you wrote:
Gen<String> arr[] = new Gen[5]; //line 1
The array variable is of type Gen<String>, but the element objects stored into the arrays are (and must be) raw types like if you said
Gen x = new Gen(1); // unparametrised. These individual element variables aren't parameterized, but you can assign parameterized Gen-objects to these like
Gen x = new Gen(1);
x = new Gen("str"); So your lines 2-4 do compile.
The fact that your array is of type Gen<String> only makes safe that the elements in the array will be casted automatically so you can say in your main method:
String s = arr[0].g; without casting.
If the array was not parameterized with String this would not work because then arr[any].g would be an Object.
However this behaviour doesn't help much because there can be any Gen<Thing> in the array as already shown.
If you do the same with
arr[1].g you would get a class cast exception.
The baseline is just: don't use arrays together with generics.
Perhaps you could also have a look at this:
http://java.sun.com/docs/books/tutorial/extra/generics/fineprint.html Yours,
Bu.