Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

type conversion

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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[]...
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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())..


 
Stephan van Hulst
Bartender
Pie
Posts: 5589
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the point? During runtime, E is unavailable, so new E() would make no sense. What should the JVM make a new instance of?

The reason E is unavailable at runtime is because otherwise old code wouldn't be compatible with Java 5+ any longer.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:What would be the point? During runtime, E is unavailable, so new E() would make no sense. What should the JVM make a new instance of?

And even if it knew, how can it be sure that E has a no-arg constructor it can use?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic