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
  • 49,592
  • 20
  • 90
  • 140
user695652
  • 3,915
  • 6
  • 36
  • 54

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,426
  • 16
  • 97
  • 154