But when the add method is called in an l-reference, you should not expect that the type Number instead of String is equal to l is a reference to ArrayList.
List l declared as an untyped list. Therefore, the compiler will allow you to insert whatever you like (and warn about it) and let you assign it to an incompatible List<String> variable (and warn about it).
If you told List<Number> l , the compiler will use the appropriate types (and not let you assign it ls ).
Regardless of what types of ads you declare or do not declare, this does not affect the execution time. To maintain backward compatibility, generics are a complete compile-time function.
Or is it that after ls = l the heap space is made from List also for l links?
If you say
List l = new ArrayList<Integer>(); List<String> ls = l; Object x = ls; Collection<?> c = (Collection) x;
you have four different variables (with four different type declarations), but they all point to the same data (the same object on the heap).
source share