• 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

Generic arrays

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,



In above code Line 1 will compile fine, but not Line 2, what difference generics can cause.

The reason could be in run time JVM can not find what list holds in line 2, if this is the reason it should be applicable to Line 1 also.So i think reason should be something else, can some body explain this please.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short explanation: You can't create an array of a generic type because of type erasure that the Java compiler performs. See this recent thread.

My blog post explains it in more detail.
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ramesh,

Generics are used for type safety at compile time, Arrays are already type safe, so there is no need to use generic for arrays. Generics are used for collections as they're not type safe.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mamadou, but that's not correct. Arrays are not "already type safe" and do not make generics unnecessary.

The only reason why you can't create an array of a generic type is because of a choice made by the people who designed the Java language and the Java compiler: they decided to make it work with type erasure.

It turns out that the combination of how arrays work in Java and type erasure leads to a conflict that cannot be cleanly resolved, so to solve the problem it was decided that arrays of generic types are simply not allowed. See my blog post (link above) for details.

It may change in a future version of Java, if we get rid of type erasure.
[ June 04, 2008: Message edited by: Jesper Young ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper,

Thanks for your reply, i am tring to understand generics in detail,so i am tring with all wierd syntax and tring to find out why it won't work, in the process i found this syntax, i know in reality we need not to use this kind of syntax.
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper for correcting me, I appreciate. I have always thought that arrays are always type safe.

Regards
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find a very detailed explanation of generics here: Angelika Langer's Java Generics FAQs
 
Hold that thought. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic