i heared that we cannot use type parameter in the array creation expression
after the compilation the statement elements=(E) new Object[size]; will be converted into elements=(Object) new Object[size]; as Object is
the default uppper bound of type parameter...then why we have to convert Object reference
into again Object...
I'm not entirely sure what you're asking, but I think your issue is with creating the Object array as an internal storage mechanism. You're right that the way Java generics are implemented, it's entirely done by compile-time enforcement. In the byte code (.class files), all traces of the generics are gone and everything is just converted to its most specific superclass, which is Object in this case. One big limitation of Java generics is that you can't instantiate a class of the generic type, like "new E()". I find that an annoyance in various situations, and I believe Java 7 is going to take some steps to improve that.
So, you're stuck instantiating an Object array, rather than an E array as you'd prefer, but does that make using generics pointless? No, not really, because your interface methods will use the generics. That is, you'll have methods like "void push(E element)" and "E pop()". These interfaces will ensure that you can only store E type elements in your Object array, and will only get E type elements back from it. That's really what you're trying to do after all. How the elements are stored internally shouldn't really affect users of your class.
Joined: Aug 02, 2010
thanks charles for your kind replay
i still have one doubt that why developers of java avoid type parameter using
in instance creation(new E())..i heard that because E is not available at run time
but E was converted into Object(default upperbound) after the coplilation.then what is
the problem with type parameter using in instance creation(new E())..