-1

I have the code:

List<Integer> grades = new ArrayList<Integer>();
ArrayList<Integer> grades1 = new ArrayList<>();

what is the difference between the first way of initializing an ArrayList and the second way?

V_S
  • 27
  • 6
  • Did you search on this at all? I've seen the same question answered here a couple times this past week. – NomadMaker Nov 25 '20 at 03:45
  • Generally you want to use ``List grades = new ArrayList();`` because this may allow you to change the type of grades to a different sort of List. – NomadMaker Nov 25 '20 at 03:47

1 Answers1

-1

Essentially, ArrayList is a kind of List, which means, as NomadMaker pointed out, ArrayLists can be initialized into Lists.

List<Integer> grades = new ArrayList<Integer>();

If you plan on changing grades to a different kind of list, you will want to use List<>, otherwise there's no difference.

  • Yes there is a difference. Sometimes the implementation has additional methods that the interface doesn't specify. In order to take advantage of those you will want to assign to the implementation type (class name) and not the interface. – WJS Nov 25 '20 at 04:03