wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics question" Watch "Generics question" New topic
Author

Generics question

david scollan
Greenhorn

Joined: Jul 14, 2005
Posts: 9
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 ]
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
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


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
david scollan
Greenhorn

Joined: Jul 14, 2005
Posts: 9
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

Joined: Aug 18, 2005
Posts: 220
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

Joined: Jul 14, 2005
Posts: 9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics question
 
Similar Threads
returning a 2-d array
Can I have a list in tree map?
KB question pg 611
annotations on entity class with spring oxm and jaxb for ArrayList
Difference between abstract and interface,not Syntactical diff