Sub topic 7
Generics and the Diamond Operator
Assume you know basic Java generics, the diamond operator (<>), and local variable declarations. This chapter focuses on how var combines with generic classes and how that combination affects the type the compiler assigns.
The initializer determines the full type
With var, Java infers a type from the initializer. The compiler does not infer only the generic interface. It records the type of the entire expression.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GenericsAndDiamond {
public static void main(String[] args) {
var names = new ArrayList<String>();
names.add("Ada");
var scores = new HashMap<String, Integer>();
scores.put("Ada", 10);
System.out.println(names); // [Ada]
System.out.println(scores); // {Ada=10}
}
}The inferred types are:
names:ArrayList<String>scores:HashMap<String, Integer>
The generic arguments are part of the static type. Therefore, names.add(42) would be rejected because the list accepts only String values.
Diamond inference with an explicit declared type
The diamond operator lets Java infer generic arguments on the right side. When the variable has an explicit type, that type provides a target for the inference.
List<String> names = new ArrayList<>();Here, the declared type is List<String>. Java uses that target type to infer that the right side means new ArrayList<String>().
The variable is still declared as List<String>, not ArrayList<String>. You can call methods declared by List, but not methods that exist only on ArrayList.
List<String> names = new ArrayList<>();
// names.ensureCapacity(10); // does not compileDiamond inference with `var`
A var declaration does not provide a target type for the initializer. The initializer must stand on its own and determine its type.
var values = new ArrayList<>();
values.add("text");
values.add(42);
System.out.println(values.get(0)); // textIn this case, the inferred type is ArrayList<Object>. Since Object can refer to both a String and an Integer, both calls to add compile.
This is often surprising:
var words = new ArrayList<>();
// String first = words.get(0); // does not compile: get returns ObjectAdding a string does not later change the list's type to ArrayList<String>. Type inference happens once, during compilation.
Use an explicit generic argument when the intended element type matters:
var words = new ArrayList<String>();
words.add("alpha");
// words.add(42); // does not compile| Declaration | Inferred or declared type | Result |
|---|---|---|
| `List<String> a = new ArrayList<>();` | `List<String>` | Safe string list |
| `var b = new ArrayList<String>();` | `ArrayList<String>` | Safe string list with concrete methods |
| `var c = new ArrayList<>();` | `ArrayList<Object>` | Values have type `Object` |
Constructor arguments can guide the diamond
The diamond can infer type arguments from constructor arguments:
import java.util.ArrayList;
import java.util.List;
public class CopyExample {
public static void main(String[] args) {
List<String> source = List.of("red", "blue");
var copy = new ArrayList<>(source);
copy.add("green");
System.out.println(copy);
}
}The constructor receives a Collection<? extends E>. The String elements in source guide inference, so copy has type ArrayList<String>.
A practical rule
Use var with a parameterized constructor when the type arguments are visible and meaningful:
var users = new ArrayList<User>();
var counts = new HashMap<String, Integer>();Avoid an untyped diamond with var when it produces Object:
var items = new ArrayList<>(); // usually unclearAn explicit type argument documents the intended data model and lets the compiler catch incorrect values.
Exercise
What is the inferred type of var data = new HashMap<>();, and what values can it store?
Show answer
Its type is HashMap<Object, Object>, so it can store keys and values of any reference type. Use new HashMap<String, Integer>() when those types should be restricted.