Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why we can't make 'generic array creation'?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This below code doesn't work


But this below code is work



The problem is 'Why the thing happen like this?'. I think the generic array creation is look quite right in my view.

When I compile with the below code (the worked version) the compile is warn me about 'unsafe and uncheck condition' but if I use the first code (the doesn't worked version) the compiler is block me from working. I don't know how to make the code both compile and don't be warn.
[ May 12, 2007: Message edited by: Tanakorn Numrubporn ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

10.3 Array Creation
An array is created by an array creation expression (�15.10) or an array initializer
(�10.6).
An array creation expression specifies the element type, the number of levels
of nested arrays, and the length of the array for at least one of the levels of nesting.
The array�s length is available as a final instance variable length. It is a compiletime
error if the element type is not a reifiable type (�4.7)
An array initializer creates an array and provides initial values for all its components.


4.7 Reifiable Types
Because some type information is erased during compilation, not all types are
available at run time. Types that are completely available at run time are known as
reifiable types. A type is reifiable if and only if one of the following holds:
� It refers to a non-generic type declaration.
� It is a parameterized type in which all type arguments are unbounded wildcards
(�4.5.1).
� It is a raw type (�4.8).
� It is a primitive type (�4.2).
� It is an array type (�10.1) whose component type is reifiable.


This is the discussion after section 10.10.

If the element type of an array were not reifiable (�4.7), the virtual machine could not perform
the store check described in the preceding paragraph. This is why creation of arrays of
non-reifiable types is forbidden. One may declare variables of array types whose element
type is not reifiable, but any attempt to assign them a value will give rise to an unchecked
warning (�5.1.9).
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find the detailed answer to your question here.
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104

This site is also a very good reference for all your generics related doubts.

thanks,
Megha

[ May 12, 2007: Message edited by: megha joshi ]

[ May 12, 2007: Message edited by: megha joshi ]
[ May 12, 2007: Message edited by: megha joshi ]
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic