java - Compile time error when adding an Integer to a generic ArrayList of String -
i know question has been asked multiple times before i'm looking answer based on type erasure.
why compiler give error on adding integer arraylist<string>? want understand type erasure , byte code of add method in arraylist.
this has nothing type erasure or byte code. compiler gives error before erases generic type parameters , generates byte code.
when adding integer arraylist<string>, compiler gives error because integer not sub-class of string.
generics add layer of type safety in compile time. if use raw arraylist instead of arraylist<string>, able add both strings , integers arraylist. however, generated byte code same regardless of whether used arraylist or arraylist<string>.
Comments
Post a Comment