hi all, can somebody help me with the folowing code snippet:
i am confused at line no1:
Gen<String> arr = new Gen; what does this line suggests with respect to what can we add to this?? i felt there is a problem in line 3 also as because we need to typecast it as we are adding it to specific generic type that is String. Am i getting the concept wrong???please help
I also find the combination of generics and arrays puzzling (at least ...).
You declared your array as: Gen<String> arr = new Gen;
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 = 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 = intgen; // no compile!
Compared to Gen<String> strgen = new Gen<String>("Hi"); //ok arr = 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.g; // no warning
in arr 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 = (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 = (Gen) new Gen(1);//line 4'
I outcommented line 5 as it does not compile: // arr = (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 ;)
Hi Burkhard, thanks for such beautiful explaination.
there are some things which are still not clear to me: like in line 3 i am putting an Integer object, so as per rule in the advanced for loop it should give me a runtime errot when i am trying to access it. but why i am not getting the exception?