开发者

Java ArrayList, generics, and late binding

Is it true to say that Java generics are all late binding? Is an ArrayList<Something> type ignored until runtime?

Why开发者_运维问答 is it?


Is it true to say that Java generics are all late binding?

No.

Is an ArrayList<Something> type ignored until runtime?

No.

Why is it?

It isn't.


Is an ArrayList<Something> type ignored until runtime?

It's rather the other way around.

Java Generics are taken into account at compile time. At runtime, there is erasure.

  • Find more details in this chapter in the Java online tutorial: Type Erasure "When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method..."


Generics and late binding doesn't have anything to do with each other.

The ArrayList type is not ignored at runtime. Its generic parameter type is often ignored through type erasure. This is done for backward compatibility.


No, Java generics are not late binding. But to quote the official Java documentation from here:

To implement generics, the Java compiler applies type erasure to: Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜