What’s the difference between defining a arraylist like this:
List list = new LinkedList();
and like this?
LinkedList list = new LinkedList();
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.
As other answers have suggested, List is the interface, and LinkedList is the actual implementation type. If, for example, you later decided that you wanted to use an ArrayList, you could type:
because ArrayList declares the interface List. The downside is that if you want to call any methods of an ArrayList/LinkedList/(Other classname that has methods that aren’t in list) later that are not part of the List interface (this probably won’t be an issue), you would have to check if it really was an ArrayList, etc. and cast it to those types. Like this:
*only after checking that it really is an ArrayList, otherwise you throw an exception.