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
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.
This is what the tutorial says:
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).Originally Posted by tutorial
The only thing I ask is that
should be interpreted asCode:E[] array = new E[100];
Code:E[] array = (E[]) new Object[100];
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Feel free to email Sun and ask them to change the language to suit your preferences.I don't agree here.
Just because I don't care doesn't mean I don't understand.
Feel free to not post in this thread unless you have something useful to say.Originally Posted by Narf
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
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.Originally Posted by Sang-drax
Just because I don't care doesn't mean I don't understand.
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.Originally Posted by Sang-drax
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
Thanks!Originally Posted by CornedBee
If I were able to write
it'd be interpreted asCode:E[] array = new E[10]But would this be unsafe? I still cannot do:Code:Object[] array = new Object[10];Code:Object someObject = ... array[2] = someObject;
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
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