Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why doesn't java allow generic arrays?

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    384
    My Mood
    Angry
    Thanks
    46
    Thanked 63 Times in 63 Posts

    Default Why doesn't java allow generic arrays?

    I can have a template class of type T in C++, just like java, but in C++ I can do this


    T[] array = new T[9];


    in java, if I do that, I get an error saying that I'm not allowed to use generic arrays.


    A lot of things like JTables and stuff allowed Vectors to be passed. Vectors were changing in size. Arrays aren't by nature.

    However, it seems Vector has become deprecated (or is close. Why Stack, which extends Vector, is still fine is a mystery to me. You'd think if a superclass became deprecated, its subclasses would also be affected by that.)

    I can use ArrayList and the toArray() method but it always must return an array of type Object.

    Hence, I might possibly run into trouble with typecasting issues (or so it seemed).

    I want to know how I can fix this so that it returns the type I need without having to do lots of type casting.


    Also, why doesn't java allow generic arrays? It's based off of C++ or (maybe it was C) but C++ allows generic arrays. Why can't java?

    Is it because of the fact that C++ doesn't really have any father superclass of all the classes but java has Object as the father superclass, hence preventing generic array or perhaps allowing it to return type Object and the creators of java used that since they were too lazy to bother with generic arrays or something, or is it something else?

    Also, is the generic thing related to java's garbage collection as java might be confused as to what to garbage collect with a generic array whereas C++ leaves the cleanup of that up to the coder?
    JavaScript is NOT Java.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,362
    My Mood
    Mellow
    Thanks
    254
    Thanked 786 Times in 771 Posts

    Default Re: Why doesn't java allow generic arrays?

    This FAQ may answer your questions, but then you have a lot of them, so it may not.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    GoodbyeWorld (October 15th, 2013)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    384
    My Mood
    Angry
    Thanks
    46
    Thanked 63 Times in 63 Posts

    Default Re: Why doesn't java allow generic arrays?

    It kind of does, but wouldn't the type-safe flaw also apply to the toArray() method that returns an array of type Object? Wouldn't that be risky too? Yet Java has that in its API. C++ does allow generic arrays. Why can't java?
    JavaScript is NOT Java.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Why doesn't java allow generic arrays?

    0.02c

    Why use a generic array when a generic List exists?
    Improving the world one idiot at a time!

Similar Threads

  1. Java Generic Types
    By New_Java_Student in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2013, 07:55 PM
  2. Java-Generic Program
    By lisha.ahuja in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 07:32 AM
  3. Design Java Generic Container-please help!!!
    By zhoujackji in forum Collections and Generics
    Replies: 1
    Last Post: November 13th, 2010, 05:55 AM