-1

Is there any reason to do this:

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

versus this:

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

I've seen the first usage a few times, and it seems to provide no benefit, which begs the question: why use it?

I understand that the diamond operator (<>) is necessary to differentiate between new LinkedList() and new LinkedList<>(), just to be clear.

cal
  • 121
  • 6
  • 3
    Possible duplicate of [What is the point of the diamond operator in Java 7?](http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7) – Grzegorz Górkiewicz Mar 31 '17 at 20:49
  • 3
    Uh, what? The diamond operator was added _after_ the other form, to let you do the `ArrayList<>` syntax instead of the longer form. You've mixed up the historical ordering. – Louis Wasserman Mar 31 '17 at 20:50
  • There is absolutely no point in not using `new ArrayList<>()`, unless the code compiles using a pre-1.7 compiler, if that's what you're asking. – M A Mar 31 '17 at 20:52

2 Answers2

0

I've seen the first usage a few times, and it seems to provide no benefit, which begs the question: why use it?

<> operator has been introduced only in JDK1.7, so you might have seen a legacy code, which used new ArrayList<Integer>() (without <> operator)

Also, the support to the code (like <Integer>) without using <> operator still exists because of backward compatibility.

So, the answer is you will not use it if your project uses JDK1.7 or later.

Vasu
  • 21,832
  • 11
  • 51
  • 67
0

The first usage with the type redundantly stated was required before Java 7 when the type inferencing provided by the diamond syntax was introduced.

David Conrad
  • 15,432
  • 2
  • 42
  • 54