• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

type conversion

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[]...
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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())..


 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic