My code is:
import java.util.*;
public class A {
public static void main(String[] args){
List<String> list = new ArrayList();
list.add("1"); //ok line 1
list.add(1); //error line 2
}
When I run this code Java gives me an error and I know why, but even when I only use line 1 the compiler warns me. Why do I get this warning? I don’t understand what is the difference between my first example and this code:
import java.util.*;
public class A {
public static void main(String[] args){
List<String> list = new ArrayList<String>(); // <-- notice the second <String>
list.add("1"); //ok line 1
list.add(1); //error line 2
}
}
When you use your code, compiler will give you a warning like
It says that you have to provide a parameterized type for your ArrayList as well.
EDIT:
As sugguested by @newacct: if you are using Java 7, you could use empty angular brackets (<>) when instantiating your Collection like:
From the Java 7 documentation: