• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Genrics Array creation

 
Ranch Hand
Posts: 8934
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading the Generics Tutorial by Gilad Bracha and found that the following code snippet causes compile error,



Since type variables don�t exist at run time, there is no way to determine what the
actual array type would be.



I dont understand why it does not compile?
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't new <T> in your code.
 
Pradeep bhatt
Ranch Hand
Posts: 8934
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by somkiat puisungnoen:
you can't new <T> in your code.



Thanks ! I dunno why I forgot this.
 
Pradeep bhatt
Ranch Hand
Posts: 8934
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


complies but


doesn't. I know why the 2nd one does not compile but why does the first code compile.
[ November 16, 2004: Message edited by: Pradeep Bhat ]
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your just allocate <T> to null, without *new*ing it?

Nick
 
Pradeep bhatt
Ranch Hand
Posts: 8934
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
Because your just allocate <T> to null, without *new*ing it?

Nick



The component type of an array may not be a parameterized type, unless it is an (unbounded) wildcard type , so why should the first code compile?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in another thread, I saw the author quote this example:


List<String> names = new ArrayList<String>();



I guess the problem might be you cannot new a list, because it is an interface, while you can new concrete class?

Nick
 
Pradeep bhatt
Ranch Hand
Posts: 8934
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
But in another thread, I saw the author quote this example:


I guess the problem might be you cannot new a list, because it is an interface, while you can new concrete class?

Nick



Nick,

I m talking about creating an array of List and not just an ArrayList.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:


complies but


doesn't. I know why the 2nd one does not compile but why does the first code compile.

[ November 16, 2004: Message edited by: Pradeep Bhat ]



Why do want to create array of List ???

I think, it's not make sense...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think, it's not make sense...


Why say so?

Nick
 
Pradeep bhatt
Ranch Hand
Posts: 8934
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by somkiat puisungnoen:


complies but


doesn't. I know why the 2nd one does not compile but why does the first code compile.

[ November 16, 2004: Message edited by: Pradeep Bhat ]<hr></blockquote>

Why do want to create array of List ???

I think, it's not make sense...[/QB]



Why not?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one answer this question.
I too came here with the same doubt, but searched the ranch if there is already an existing thread on "Generic Array Creation" error.

I tried this


and got the error: Generic Array Creation.

Can some body please explain why i can't create an array of LinkedLists which can operate on strings.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by VarunKumar Mallisetty:
Can some one answer this question.
I too came here with the same doubt, but searched the ranch if there is already an existing thread on "Generic Array Creation" error.

I tried this


and got the error: Generic Array Creation.

Can some body please explain why i can't create an array of LinkedLists which can operate on strings.



If you were allowed to do it, you could do the following:



This neither leads to a compile time error nor to a runtime error, because only at runtime can be checked whether the last assignment is allowed (you'd get an ArrayStoreException if not), but the type parameter for the LinkedList is erased at runtime.

A more elaborate explanation can be found at http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot create an array of a parameterised type unless you use the unbounded wildcard (?) or you generate a compile-time warning. This applies to (for example) java.util.ArrayList - where it is impossible to implement without a compile-time warning.

I pointed this out to a member of JSR-14 way back when (when I was working on the implementation and JSR-14 was an infant) - and in particular that arrays and collections are no longer convertible will cause a headache for many. The response was something along the lines of "oops".

This is (yet another) example of the shifting line/blur that Sun calls "reverse compatibility". There are many out there who proclaim a definitive contract that denotes what can and cannot change as J2SE continues to progress, however, a more detailed analysis proves that this line regularly shifts to meet marketing demands. I think that this one was a screw up on the behalf of the implementers however - since it would seem to violate what the market demands and would inevitably cost. At the time that I humbly pointed it out (it was coincidental that it even came up in conversation), it was probably too late since changing it would have had a propagating effect.

Begin Edit.
@SuppressWarnings didn't work at that time so it was not an option.
End Edit.
[ July 08, 2006: Message edited by: Tony Morris ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!