• 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

Java Generics and Varargs

 
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Why does ts.getClass().getComponentType() return the type before erasure in the case of varargs?
That is instead of creating an array of Object because the type parameter is not bounded, I succeed in creating an array of T.

Where can I read about this in the specification or tutorials?

Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramsin Khoshaba wrote:
Why does ts.getClass().getComponentType() return the type before erasure in the case of varargs?



Your code is simply an example of creating an array via the reflection library (specifically, via the Array class). This method of creating arrays works, even without generics and / or varargs involved.

Henry
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Ramsin Khoshaba wrote:
Why does ts.getClass().getComponentType() return the type before erasure in the case of varargs?



Your code is simply an example of creating an array via the reflection library (specifically, via the Array class). This method of creating arrays works, even without generics and / or varargs involved.

Henry



Yea, but why is not
ts.getClass().getComponentType() == Object.class
true when I use the type argument String for T?

Why isn't type erasure taking place in this case?
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type-erasure is happening, your makeArray method has compiled the type (Object[])Object[], but the generic types are still present during compile-time -> you create and pass a String array to the method (and thus receive a String array).
Your code is compiled similar to



 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tobias Bachert wrote:Type-erasure is happening, your makeArray method has compiled the type (Object[])Object[], but the generic types are still present during compile-time -> you create and pass a String array to the method (and thus receive a String array).
Your code is compiled similar to



Thank you very much.

So the compiler translates the argument list to the equivalent array creation expression before type erasure.


This prints Comparable, even though I have Strings.
So it is the inferred type that counts, not the actual type.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the runtime (actual) type. For instance, try the following:

In your previous example, because of the explicit generic type, the varargs type became Comparable<String>..., which is the same as Comparable<String>[], which is (because of type erasure) the same as Comparable[]. If you pass in something that is a String[], even though the inferred type is Comparable<String>[], the runtime type is String[], and so the component type will be String.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, I can make your code shorter, because varargs are actually little less than syntactic sugar around arrays:
And if you don't mind that the input is modified, you can even simply return ts.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic