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

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -