DEV Community

Pramod Bablad
Pramod Bablad

Posted on

Java 9 Diamond Operator Improvements

Diamond Operator : Before Java 7

Before Java 7, you need to explicitly mention type on both side of the declaration statement.

List list = new ArrayList();
Set set = new HashSet();
Map map = new HashMap();

Diamond Operator : After Java 7

With the introduction of empty diamond operator <> from Java 7, you need not mention type on the right side of the declaration statement. You can leave empty inside the diamond operator. Java compiler automatically determines the type on the right side of the declaration statement.

List list = new ArrayList<>();
Set set = new HashSet<>();
Map map = new HashMap<>();

But, that rule doesn’t apply to anonymous inner classes. Empty diamond operator can not be used with anonymous inner classes.

This issue has been resolved from Java 9.

From Java 9, you can use empty diamond operator <> for anonymous inner classes also.

Source : https://javaconceptoftheday.com/java-9-diamond-operator-improvements/

Top comments (0)