• 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

Generics question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't instantiate an unbounded object ie
List<String> myList1 = (List<String>) new List<?>(); // Compile error
so how come we can instantiate an array of unbounded objects ie
List<String>[] myList2 = (List<String>[]) new List<?>[4]; // Ok
[ October 07, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure that code compiles without an error?
I'm not sure u can use
List<String>[] //this

anyway, you will find heaps of details about why you cannot and other gimmicky stuff at
http://angelikalanger.camelot.de
at the generics FAQ section, read it and convince yourself
 
david scollan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akshay
List<String>[] myList2 = (List<String>[]) new List<?>[4];
compiles ok in Eclipse SDK 3.1. Why do you say it's not ok?
Regards
DS
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by david scollan:
Akshay
List<String>[] myList2 = (List<String>[]) new List<?>[4];
compiles ok in Eclipse SDK 3.1. Why do you say it's not ok?
Regards
DS



I'm sorry, the previous link was supposed to be
http://www.langer.camelot.de/GenericsFAQ/JavaGenericsFAQ.html
that.
Anyway, the code compiles, I mean, it throws unchecked warnings. And if you're using generics, then your prime concern is to get the code compiled without any warnings regarding generic usage.

To answer your question, please read
http://www.langer.camelot.de/GenericsFAQ/FAQSections/ParameterizedTypes.html#How%20can%20I%20work%20around%20the%20restriction%20that%20there%20are%20no%20arrays%20whose%20component%20type%20is%20a%20concrete%20instantiation%20of%20a
 
david scollan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link, there's stacks of info on this site. The answer they give is -:

Why is it allowed to create an array whose component type is an unbounded wildcard parameterized type?
Because it is type-safe.
The rationale is related to the rule for other instantiations of a generic type: an unbounded wildcard parameterized type is a reifiable type and arrays of reifiable types are type-safe, in contrast to arrays of non-reifiable types, which are not safe and therefore illegal. The problem with the unreliable array store check (the reason for banning arrays with a non-reifiable component type) does not occur if the component type is reifiable.
Example (of array of unbounded wildcard parameterized type ):

Object[] pairArr = new Pair<?,?>[10] ; // fine
pairArr[0] = new Pair <Long,Long>(0L,0L); // fine
pairArr[0] = new Pair <String,String>("",""); // fine
pairArr[0] = new ArrayList <String>(); // fails with ArrayStoreException
The array store check must check whether the element added to the array is of type Pair<?,?> or of a subtype thereof. In the example the two pairs, although of different type, are perfectly acceptable array elements. And indeed, the array store check, based on the non-exact runtime type Pair , accepts the two pairs and correctly sorts out the "alien" ArrayList object as illegal by raising an ArrayStoreException . The behavior is exactly the same as for an array of the raw type, which is not at all surprising because the raw type is a reifiable type as well.
reply
    Bookmark Topic Watch Topic
  • New Topic