3

Lets assume that I have a variable:

List<String> myList;

Is there a difference between these two lines?

myList = new ArrayList<>();
myList = new ArrayList<String>();

I guess both does the same on compilation, but still I'm not sure.

Thanks in advance

iGoodie
  • 140
  • 1
  • 9

1 Answers1

3

This is called Type inference for Generics and was added in Java 7.

Both versions of the code you wrote are the same.

There are a few caveats you might want to read about here: https://docs.oracle.com/javase/8/docs/technotes/guides/language/type-inference-generic-instance-creation.html

Kon
  • 10,477
  • 6
  • 37
  • 56