Java Diamond Operator: Enhancing Code Readability with Generics
Learn about the Java diamond operator introduced in Java 7, which simplifies the syntax for working with generics. Discover how this feature improves code readability when instantiating collections like ArrayList
, and see practical examples of its usage in Java programming.
Java Diamond Operator
The diamond operator was introduced in Java 7 to make code more readable when working with Generics. Generics allow us to pass any kind of object to be processed by class methods. For example, before Java 7, we had to use the following syntax to instantiate a list of strings with an ArrayList
object:
Syntax
List<String> listOfStrings = new ArrayList<String>();
From Java 7 onwards, we can simplify the syntax using the diamond operator:
Syntax
List<String> listOfStrings = new ArrayList<>();
Limitation Before Java 9
Before Java 9, the diamond operator could not be used with Anonymous inner classes. For example, the following code requires specifying the type argument explicitly:
Syntax
Handler<Integer> intHandler = new Handler<Integer>(1) {
@Override
public void handle() {
System.out.println(content);
}
};
Diamond Operator in Anonymous Classes (Java 9)
In Java 9, the diamond operator can be used with anonymous classes to improve readability. Here is the updated syntax:
Syntax
Handler<Integer> intHandler = new Handler<>(1) {
@Override
public void handle() {
System.out.println(content);
}
};
Diamond Operator in Java 7 and Java 8
In the example below, we create anonymous classes for an abstract class Handler
accepting a generic argument. Notice that before Java 9, we must pass the type argument explicitly:
Syntax
public class Tester {
public static void main(String[] args) {
Handler<Integer> intHandler = new Handler<Integer>(1) {
@Override
public void handle() {
System.out.println(content);
}
};
intHandler.handle();
Handler<? extends Number> intHandler1 = new Handler<Number>(2) {
@Override
public void handle() {
System.out.println(content);
}
};
intHandler1.handle();
Handler<?> handler = new Handler<Object>("Test Message") {
@Override
public void handle() {
System.out.println(content);
}
};
handler.handle();
}
}
abstract class Handler<T> {
public T content;
public Handler(T content) {
this.content = content;
}
abstract void handle();
}
Output
1
2
Test Message
Diamond Operator in Java 9 and Beyond
Starting with Java 9, we can use the diamond operator with anonymous classes without specifying the type argument. The compiler infers the type automatically:
Syntax
public class Tester {
public static void main(String[] args) {
Handler<Integer> intHandler = new Handler<>(1) {
@Override
public void handle() {
System.out.println(content);
}
};
intHandler.handle();
Handler<? extends Number> intHandler1 = new Handler<>(2) {
@Override
public void handle() {
System.out.println(content);
}
};
intHandler1.handle();
Handler<?> handler = new Handler<>("Updated Test Message") {
@Override
public void handle() {
System.out.println(content);
}
};
handler.handle();
}
}
abstract class Handler<T> {
public T content;
public Handler(T content) {
this.content = content;
}
abstract void handle();
}
Output
1
2
Updated Test Message