What is the difference between
List list = new ArrayList<Integer>();
And
List<Integer> list = new ArrayList<Integer>();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By doing:
you are not telling the compiler what kind of List it is.
This would allow someone to compile this code:
which is clearly wrong and would cause you exception, because you can add anything in the list, so the compiler is not sure if list.get(0) is really an Integer.
Now with this way you are telling the compiler that this list will only accept and hold integers or Integer subclasses (in case it could be subclassed).
so this wouldn’t compile: