2

I came across the following code and was wondering whether this is good practice:

LinkedList<String> ll = new LinkedList(); 

I would have written

List<String> l = new LinkedList<String> (); 

So there are two questions here:

  1. I always thought its good practice to use the type List and not ArrayList/LinkedList.
  2. What exactly happens if you omit the diamond on the RHS of the assignment operator?
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
user695652
  • 4,105
  • 7
  • 40
  • 58

1 Answers1

5
  1. Always program to interfaces, that way your code stays implementation independent.
  2. You are creating the raw type, which will give you a warning, but it will compile. "LinkedList is a raw type. References to generic type LinkedList should be parameterized". It doesn't actually matter though because of erasure.
Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158