Is it ok to omit diamond operator in recent java versions (8+)?

apadana :

With Java 7 we could use the diamond operator:

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

Now I am seeing in some recent codes people dropping the diamond operator:

List<String> list = new ArrayList();

Is this a new feature added in more recent java versions (Java 8+) to drop the diamond operator? Or they are simply relying on the raw old ArrayList type (which just happens to work for backward compatibility reasons, and should be avoided)?

Marius :

The correct syntax (for what you want) is:

List<String> l1=new ArrayList<>();

Omitting the diamond operator means an unqualified list, which (under the hood is the same, of course, but) will give you a compiler warning.

Using it unqualified works against the compile-time type-checking system, and is not in accordance with the variable you declare with the diamond operator, but it will compile (and work if you do it right). That's why you will get a warning.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403894&siteId=1