aspose file tools*
The moose likes Java in General and the fly likes type conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "type conversion" Watch "type conversion" New topic
Author

type conversion

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84


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 Love Java Ranch"
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

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

Joined: Aug 02, 2010
Posts: 84
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: type conversion