Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Why Didn't The Creators Of Java Allow For Generic Array Intialization?

Thread view: 
res7cxbi@verizon.net - 02 Jan 2006 03:38 GMT
Just Curious...

Why can't i initialize a generic array like this?

private E[] data = new E[10];

This creates a compiler error saying that i can't intialize generic
arrays
The only workaround i've seen (the Collections class do this too) is
this:

private E[] data = (E[]) new Object[10];

so the question is...

Why Didn't The Creators Of Java Allow For Generic Array Intialization?
Stefan Schulz - 02 Jan 2006 06:20 GMT
On Sun, 01 Jan 2006 19:38:55 -0800, res7cxbi wrote:

> Just Curious...
>
[quoted text clipped - 12 lines]
>
> Why Didn't The Creators Of Java Allow For Generic Array Intialization?

Because the instruction new E[] would have to become new Object[] (or
whatever) "below the cover", which means that any Object reference could
be stored. While this was once deemed appropriate (since Object [] can be
downcast to any specific array type), it is no longer considered valid.

So, for now we will have to explicitly agree to store any and all kinds of
Objects (new Object[]), and create a more specific view (cast to E[]) of
it. What is annoying is that this technique will still generate a warning
which can only be handled with a @SuppressWarning annotation (if it is
supported by your compiler).

Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.