3 ms·
All generics in Java are of type Object from the start, you can't call any methods on them not implemented by Object. So it is wrong to say they are erased, the
by username90 7y ago
All generics in Java are of type Object from the start, you can't call any methods on them not implemented by Object. So it is wrong to say they are erased, they were never there to start with.
- vbezhenar 7y agoAnd that's why Java is slower than it could be. If I'm using String[] array, it can't contain anything but String objects, so JVM does not have to check type everytime I'm accessing that array. That's not true for ArrayList<String>, where compiler must check returned object type from `get(index)` (because it really is Object and can contain anything), but it could be true with better language design.
- titzer 7y agoYou can add a constraint to a generic declaration, which is an upper (or lower) bound on the allowable type arguments. e.g. class A<Y extends X> { // in this scope, Y is known to be of at least type X, // so, we can call methods on expressions of type Y // that belong to type X (and not Object) Y m() { ... } } Type arguments are omitted from usage sites. The technique is literally called erasure in papers and documentation. a = new A<Foo>(); f = a.m(); // should return a Foo, in bytecode returns Y // and compiler inserts a cast from Y -> Foo Generic code is slower in Java because of these extra casts. To get back (most of) the performance, the JVM has to inline enough methods to be able to track the types from start to finish. It can't always.