Howdy Debasmita!
I also find the combination of generics and arrays puzzling (at least ...).
You declared your array as:
Gen<String> arr[] = new Gen[5];
This line compiles, but gives a warning. The warning comes up because the thing is not type safe. And you already proved that. You put something wrong into the array with:
arr[1] = new Gen(1); //line 3 (warning)
It is not type safe, because it produces
only a warning, but it compiles. The fact that it compiles shows that it isn't type safe. The warning also comes when you put something decent in it, like in line 2.
You asked, what can be stored in and get out of such an array.
IN
We saw it already, you can store
anything in it. Or better, you can store only Objects. Because of
polymorphism (remember:
Object o = new String("howdy!"); ) you can store anything.
OUT
Here comes the problem: you can get only Strings out of the elements, as the variable is declared as Gen<String> arr[].
PROBLEM
With these arrays you can store anything, but it guarantees that only Strings will get out of the Gen elements at the places of the array. Did I said it already? This is not type safe.
Half Cooked:
There
is some kind of type safety in this "generic arrays".
For example the following would not compile, if you hang it at the end of the main method of class Gen:
Gen<Integer> intgen = new Gen<Integer> (new Integer(5)); // ok, no warning
arr[4] = intgen; // no compile!
Compared to
Gen<String> strgen = new Gen<String>("Hi"); //ok
arr[4] = strgen; // no warning! (and compiles happily)
Here place #4 of the Gen<String> arr[] is refered to a Gen<String> variable, which is perfectly ok, and doesn't produce a warning. For variable strgen only can contain Strings (as Gen's variable named g). Gen's variable g will always be a String, it is safe to return only Strings.
Have a cup of tea and compare this situation with your line 2 (that gave a warning, but we know it is safe in this case).
You may also add the following line to the end of your main method to prove the warnings make sense:
String s = arr[1].g; // no warning
in arr[1] you stored a Gen parameterized with Integer, the parametirization of variable arr secures the return of a String, compiler compiles (without !!! a warning) but at runtime you get a class cast exception. In this line there is no warning, because compiler thinks, there are only Strings in it. But you were warned earlier (lines 1 and 3).
Something different, your casts, first line 4:
arr[2] = (Gen<String>) new Gen(1);//line 4
Line 4 causes a warning, but an other warning than before.
It has nothing to do with arrays.
Compiler just tells you that he is actually casting against the raw type of Gen, i.e. it is the same as if you have written:
arr[2] = (Gen) new Gen(1);//line 4'
I outcommented line 5 as it does not compile:
// arr[3] = (Gen<String>) new Gen<Integer>(1); //line 5
In spite of the fact, that the casting is (at the end) only against the raw type, compiler refuses to compile this line and reports inconvertible types.
Nice compiler.
By the way, I repaired this lines (4&5), as I think it should have been. With some combination of symbols, e.g. ;) there will be smilies instead of code. When you see this, you may hit the edit-Button(notepad icon) and resend your posting with a tick in "Dissasemble smilies..." below the text box for posting. Smilies are also disassembled here, otherwise you see a smiley (or graemlin how they're called here) instead of ;)
How was the tea?
Bu.