java - Need clarification on addAll methods behaviour -
hi using addall method of collection framework. please find below code. working fine code 1. code 2 giving me compilation error. dont know why didnt give me error code 1. kindly give reason this.
code 1
public static void main(string[] args) { list<integer> firstlist=new arraylist<integer>(); list secondlist=new arraylist(); //without generic secondlist.add("string value"); firstlist.addall(secondlist); system.out.println(firstlist); } output:
[string value] code 2
public static void main(string[] args) { list<integer> firstlist=new arraylist<integer>(); list<string> secondlist=new arraylist<string>(); //with generic secondlist.add("string value"); firstlist.addall(secondlist); system.out.println(firstlist); } output
compilation error
java generics checked on compile time. means compiler can check generic list , can show error if string list integer. while in first case . non-generic, compiler cannot judge @ compile time.
read type erasure
Comments
Post a Comment