diamond operator
The empty angle brackets in new ArrayList<>(), which ask the compiler to infer the constructor's type arguments.
In more detail
The type comes from the declaration on the left, so you do not repeat it. Leaving the brackets off entirely is different: that creates a raw type and loses the compiler checks.
Example
List<Number> values = new ArrayList<>();Related terms
- type inference — The compiler working out a type from the code itself, so you do not have to write it out by hand.
- raw type — A generic type used without any type arguments, such as plain List, which turns off the compiler's type checks for it.
- generics — Types that take other types as parameters, so one definition works for many kinds of value.
Taught in
- The Collections Framework — Modern Java: Virtual Threads & High-Throughput Services
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- Fifty Items, Five Traps — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Scope, var And Immutable Objects — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Concurrent Collections And Parallel Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep