Java SE 7—Diamond (<>) Notation for Creating an Object of a Generic Class

Consider line 10 of Fig. 7.24:

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

Notice that ArrayList<String> appears in the variable declaration and in the class instance creation expression. Java SE 7 introduced the diamond (<>) notation to simplify statements like this. Using <> in a class instance creation expression for an object of a generic class tells the compiler to determine what belongs in the angle brackets. In Java SE 7 and higher, the preceding statement can be written as:

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

When the compiler encounters the diamond (<>) in the class instance creation ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.