Search code examples
javagenericsvarlocal-variablesjava-10

How do "var" and raw types come together?


I came across an answer that suggests to use

var list = new ArrayList();

I was surprised to find a raw type here, and I am simply wondering: does var use the <> "automatically?

( in the meantime, the answer was changed to use <String>, but I am still curious about but "principles" here )

I saw other questions such as this, but they all use the diamond operator:

var list = new ArrayList<>();

Now I am simply wondering: does var change how we should (not) be using raw types? Or is that suggestion to leave out <> simply bad practice?


Solution

  • I came across an answer that suggests to use...

    I would ignore that answer, because as you point out, it uses raw types and it types list as specifically ArrayList. (Update: The answerer edited their answer to add an element type.) Instead:

    List<AppropriateElementType> list = new ArrayList<>();
    

    According to the second answer you linked, var will cause the compiler to infer an element type from the right-hand side if you include the <>, picking the most specific type it can. In var list = new ArrayList<>(); that would be ArrayList<Object>, though, because it doesn't have anything more specific it can choose.

    But, this:

    var list = new ArrayList();
    

    ...without the <>, is using a raw type (ArrayList), not a parameterized type with Object as the parameter (ArrayList<Object>), which is different.


    If the use of list is sufficiently contained (a few lines in a method), having it typed ArrayList<X> rather than List<X> may be acceptable (depends on your coding style), in which case:

    var list = new ArrayList<AppropriateElementType>();
    

    But generally I prefer to code to the interface rather than the concrete class, even with locals. That said, with locals, it is less important than with instance members, and var is convenient.