ArrayList aList = new ArrayList();
List aList = new ArrayList();
What\'s the difference between these two and which is better to use and why?
If you are using Java 1.4 or earlier, then I would use the second one. It's always better to declare your fields as generically as possible, in case you need to make it into something else later on, like a Vector, etc.
Since 1.5 I would go with the following
List x = new ArrayList();
It gives you a little bit of type safety. List 'x' can add a String Object, and that's it. When you get an item from the List 'x', you can count on the fact that a String is going to come back. This also helps by removing unnecessary casting, which can make code hard to read when you go back 6 months later and try to remember what your code does. Your compiler/IDE will help you to remember what type should be going in to List 'x' by displaying an error if you try to add any other Object type.
If you want to add multiple Object types to a List then you could add an Annotation to suppress the compile error
@SuppressWarnings("unchecked")
List y = new ArrayList();