• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generic Arrays

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some body tell why Java does not support generic arrays with a simple example. What made Sun Micro to disallow this feature?
 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume that it would be possible. Take a look at this example:
Because of type erasure, the actual type T is no longer known during runtime. It gets substituted by the first bound for T, or Object if there is no bound. For example, if it's <T> then T becomes Object, if it's <T extends Number> then T becomes Number, if it's <T extends Serializable & Number> it becomes Serializable.

Let's assume you have no bounds. That makes ts an actual Object[]. The assignment to objects[0] will therefore succeed. If T is Integer though you will then get a ClassCastException when accessing the elements through ts. The entire type protection of arrays is gone.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic