Sub topic 4
The Diamond Operator and Type Inference
In the last chapter you wrote Box<String> box = new Box<String>("hi");. That line names String twice. Java can work out the second one for you. This chapter shows how.
What the Diamond Operator Is
The diamond operator is the empty pair of angle brackets <>. You put it after the class name when you create an object. It tells the compiler to fill in the type arguments by itself.
Type inference is the compiler working out a type from the code around it, so you don't have to write it.
Here are two lines that mean exactly the same thing:
import java.util.ArrayList;
import java.util.List;
public class DiamondDemo {
public static void main(String[] args) {
List<String> longForm = new ArrayList<String>(); // type written twice
List<String> shortForm = new ArrayList<>(); // diamond: compiler fills in String
longForm.add("apple");
shortForm.add("banana");
// shortForm.add(42); // compile error: the list holds String
System.out.println(longForm.get(0) + " " + shortForm.get(0));
}
}The diamond does not turn off type checking. shortForm is still a List<String>, and adding 42 still fails to compile.
How the Compiler Decides
The compiler looks at where the new object goes. On the left side you declared List<String>, so the object on the right must be an ArrayList<String>.
The diamond works anywhere the compiler can see the target type, such as a return statement:
import java.util.HashMap;
import java.util.Map;
public class Inventory {
static Map<String, Integer> emptyStock() {
return new HashMap<>(); // inferred as HashMap<String, Integer>
}
public static void main(String[] args) {
Map<String, Integer> stock = emptyStock();
stock.put("pens", 12);
System.out.println(stock); // {pens=12}
}
}A Common Trap: `var` with `<>`
The var keyword (Java 10 and later) asks the compiler to work out the variable's type from the right side. If the right side is also a bare diamond, neither side names a type. The compiler then falls back to Object.
import java.util.ArrayList;
public class VarTrap {
public static void main(String[] args) {
var things = new ArrayList<>(); // ArrayList<Object>
things.add("text");
things.add(99); // allowed: anything is an Object
var names = new ArrayList<String>(); // ArrayList<String>
names.add("Ada");
// names.add(99); // compile error
System.out.println(things + " " + names);
}
}Quick Rules
| You write | Compiler infers |
|---|---|
| `List<String> a = new ArrayList<>();` | `ArrayList<String>` |
| `Map<String, Integer> m = new HashMap<>();` | `HashMap<String, Integer>` |
| `var b = new ArrayList<>();` | `ArrayList<Object>` |
| `var c = new ArrayList<Integer>();` | `ArrayList<Integer>` |
The compiler can also infer type arguments for methods from the values you pass in. You will see that in the chapter on generic methods.
Exercise
What type does pairs have in Map<String, Double> pairs = new HashMap<>();, and will pairs.put("pi", "3.14"); compile?
Show answer
pairs holds a HashMap<String, Double>, inferred from the declared type. The put call does not compile, because "3.14" is a String and the map's values must be Double.
Exercise
Rewrite var scores = new ArrayList<>(); so that scores can hold only Integer values.
Show answer
Name the type argument on the right side: var scores = new ArrayList<Integer>();. You can also drop var and write List<Integer> scores = new ArrayList<>();.