Sunday, 15 June 2014

Java Generics and Raw Types -



Java Generics and Raw Types -

i have next code:

class="lang-java prettyprint-override">arraylist value = new arraylist<integer>(); // 1 value.add("test"); // 2

i'm trying understand line 2. although can see value.add("test"); compiles without errors, can't see reason doesn't throw runtime exception. if value referencing generic arraylist object, why java allows add together string it? can explain me?

the closest explanation i've found described here, still don't understand core reason:

stack s = new stack<integer>()

this legal conversion parameterized type raw type. able force value of type. however, such operation result in "unchecked call" warning.

generic types erased during compilation. @ runtime, arraylist raw arraylist, no matter if defined generic or not.

in case, code compiles arraylist declaration not generic, , runs fine because of type erasure.

java generics raw-types

No comments:

Post a Comment