Thread: Java: Why isn't this allowed?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Read section 7.3 in the following tutorial for why this isn't allowed:
    http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
    Just because I don't care doesn't mean I don't understand.

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This is what the tutorial says:
    Quote Originally Posted by tutorial
    Similarly, attempting to create an array object whose element type is a type variable causes a compile-time error:
    Code:
    <T> T[] makeArray(T t) { return new T[100]; // error
    }
    Since type variables don’t exist at run time, there is no way to determine what the actual array type would be.
    I don't agree here. Sure, the type T isn't known, but no T object is created, only references (100 of them) to T objects are created (and set to null).

    The only thing I ask is that
    Code:
    E[] array = new E[100];
    should be interpreted as
    Code:
    E[] array = (E[]) new Object[100];
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    I don't agree here.
    Feel free to email Sun and ask them to change the language to suit your preferences.
    Just because I don't care doesn't mean I don't understand.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Narf
    Feel free to email Sun and ask them to change the language to suit your preferences.
    Feel free to not post in this thread unless you have something useful to say.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Quote Originally Posted by Sang-drax
    Feel free to not post in this thread unless you have something useful to say.
    My suggestion was very serious. I'm sorry that you don't approve of reality, but if you don't like it, you can suggest changes to Sun. But thanks for your rude and insulting response anyway. I always appreciate being wrongly flamed by ..............s.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Sang-drax
    The only thing I ask is that
    Code:
    E[] array = new E[100];
    should be interpreted as
    Code:
    E[] array = (E[]) new Object[100];
    But the (E[]) cast is a no-op. E is (unless it's defined as 'extends XXX') completely replaced by Object in the resulting bytecode. All you're doing is casting an object array to an object array. The only effect of generics are the casts the compiler writes into the code automatically.

    Allowing "new E[10]" would lead to the misconception that the runtime type of the array would be E[]. It isn't. It's Object[], which cannot be cast to any more specialized array type. For example, this code throws a class cast exception:
    String[] bla = (String[]) new Object[10];

    On the other hand, this code is valid, the runtime type being String[]:
    String[] bla = new String[10];

    Allowing this:
    E[] bla = new E[10];
    would lead people to believe that the runtime type is E[]. This can be dangerous. And that's why it's not allowed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by CornedBee
    The only effect of generics are the casts the compiler writes into the code automatically.
    Thanks!

    If I were able to write
    Code:
    E[] array = new E[10]
    it'd be interpreted as
    Code:
    Object[] array = new Object[10];
    But would this be unsafe? I still cannot do:
    Code:
    Object someObject = ...
    array[2] = someObject;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The equals method is not generic. It always takes an Object parameter.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java for real-time applications
    By zacs7 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-26-2008, 06:34 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  4. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  5. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM