Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Which lines compile and what types are inferred?

Java
List<String> a = new ArrayList<>();
var b = new ArrayList<>();
List<String> c = Collections.emptyList();
var d = List.of(1, 2.0);

All four lines compile. For a, the diamond infers ArrayList<String> from the target type List<String>; for b, var gives the diamond no target, so it infers ArrayList<Object>; for c, the generic method emptyList() takes T = String from the target type and returns List<String>; and for d, List.of(1, 2.0) infers the element type as an intersection of the common supertypes of Integer and Double, roughly Number & Comparable<...> (plus Constable & ConstantDesc on Java 12 and later).

Why this question is asked

Type inference is the compiler working out types we did not write. It saves typing, but it also surprises people when var and the diamond meet, or when the compiler picks a type nobody can write by hand. Interviewers ask this coding question to see whether we can predict what the compiler will infer and know where inference gets its information. In this answer, we will recap the two sources of inference, then go line by line, checking each inferred type with a small experiment, and finish with what this means for everyday code.

1Where Inference Gets Its Information

Java infers type arguments from two places. The first is the arguments: in List.of(1, 2), the compiler looks at the argument types to decide T. The second is the target type, the type the expression is expected to have because of where it appears, such as the declared type of the variable it is assigned to or the parameter type of the method it is passed to.

The diamond, the empty <> in new ArrayList<>(), asks the compiler to infer the class's type arguments, mostly from the target type. var asks the compiler to infer a local variable's type from its initialiser. When we combine them, each one is waiting for the other to provide a type, and that is where line b gets interesting.

Java
List<String> a = new ArrayList<>();
var b = new ArrayList<>();
List<String> c = Collections.emptyList();
var d = List.of(1, 2.0);
What each line infers
LineSource of the typeInferred type
atarget type List<String>ArrayList<String>
bno target, nothing to go onArrayList<Object>
ctarget type of a generic methodList<String>, T = String
darguments Integer and DoubleList<Number & Comparable<...> & ...>
All four compile; only d infers a type we could not easily write ourselves.

2Line a: Diamond With a Target Type

The variable is declared as List<String>, so the expression new ArrayList<>() has a target type. The compiler looks for type arguments for ArrayList that make it compatible with List<String>, and the only answer is String.

Java
List<String> a = new ArrayList<>(); // ArrayList<String>
a.add("x"); // ok
a.add(1); // error: int cannot be converted to String

This is the main use of the diamond, added in Java 7: we state the type once, on the left, and the right side follows.

3Line b: var Removes the Target

With var, the variable has no declared type; its type will be whatever the initialiser has. So the diamond has no target to learn from, and there are no constructor arguments that mention the element type either. With no constraint at all, the compiler falls back to the bound of ArrayList's type parameter E, which is Object.

Java
var b = new ArrayList<>(); // ArrayList<Object>
b.add("x"); // ok
b.add(1); // ok: anything goes
String s = b.get(0); // error: Object cannot be converted to String
var better = new ArrayList<String>(); // ArrayList<String>: put the type on the right

Notice that line b compiles but has quietly lost the element type. The rule of thumb is that with var the type argument must be written on the right side, because the left side no longer provides it.

4Line c: A Generic Method and Its Target

Collections.emptyList() is a generic method, declared as public static final <T> List<T> emptyList(). It has no arguments, so the arguments cannot tell the compiler what T is. The target type List<String> can: the compiler chooses T = String so the result fits.

Java
List<String> c = Collections.emptyList(); // T = String
List<Integer> n = Collections.emptyList(); // T = Integer, same method
var e = Collections.emptyList(); // List<Object>: no target again
List<String> f = Collections.<String>emptyList(); // explicit type argument, also fine

Assignment targets have been used for this since Java 5. Since Java 8 the same works when the call is a method argument, for example printAll(Collections.emptyList()) where printAll takes a List<String>, which earlier versions rejected.

5Line d: An Intersection Type From Mixed Arguments

List.of(1, 2.0) has arguments, so inference uses them. The 1 is boxed to Integer and the 2.0 to Double. T must be a supertype of both, and the compiler picks the most specific one it can, called the least upper bound. Integer and Double share several supertypes: Number, Comparable of something, and, since Java 12, the constant API interfaces Constable and ConstantDesc. None of them is a subtype of all the others, so the inferred type is an intersection of them all.

Java
var d = List.of(1, 2.0);
// inferred: List<Number & Comparable<? extends Number & Comparable<?> & ...> & Constable & ConstantDesc>
Number first = d.get(0); // ok: every element is a Number
double sum = d.stream().mapToDouble(Number::doubleValue).sum(); // 3.0
d.get(0).compareTo(d.get(1)); // compiles, but throws ClassCastException at run time
List<Number> nums = List.of(1, 2.0); // with a target, T = Number: a simpler type

The intersection type exists only in the compiler; we cannot write it in a declaration, but var can hold it, and we can use any of its parts. Notice the compareTo call: it compiles because the type says the elements are Comparable, but an Integer cannot be compared with a Double, so it fails at run time. Giving the variable an explicit type such as List<Number> avoids such strange types, and is usually what we meant.

6Follow-up questions

Is var b = new ArrayList<>() a bug?

It is not an error, but it is almost always a mistake, because the list accepts any object and every read needs a cast. Many style guides and IDE inspections flag var combined with the diamond for exactly this reason.

Does var make Java dynamically typed?

No. The variable still has a fixed static type, chosen once by the compiler from the initialiser. var only saves us from writing it. Assigning a value of an incompatible type later is still a compile error.

Why does the compiler infer an intersection rather than Object for line d?

Because inference picks the most specific common supertype, and Object would throw away useful information such as the fact that every element is a Number. The intersection keeps all the shared supertypes, which is why d.get(0).doubleValue() compiles.

7Conclusion

In this answer, we saw that all four lines compile, and that each inferred type depends on where the compiler finds information. The diamond on line a uses the target type to infer ArrayList<String>; on line b var removes the target, so the diamond falls back to ArrayList<Object>; on line c the generic method emptyList() takes T = String from its target; and on line d the arguments Integer and Double give an intersection type built from Number, Comparable and, on recent JDKs, Constable and ConstantDesc. When the inferred type is not what we want, writing it explicitly fixes it.

What to say in the interviewAbout 80 seconds aloud

Open with

All four lines compile, and the interesting part is where each type comes from. Line a: the diamond looks at the target type, List<String>, so it infers ArrayList<String>.

Then, if they want more

Line b: var takes its type from the initialiser, and the diamond takes its type from the target, so they have nothing to give each other; with no constraints the compiler uses E's bound, so b is ArrayList<Object>.

It compiles, but the list accepts anything and reads come back as Object, so with var I write the type argument on the right, new ArrayList<String>(). Line c: emptyList is a generic method with no arguments, so T comes from the target type, and c is List<String>. If I wrote var e = Collections.emptyList(), I would get List<Object>.

Line d: here the arguments drive inference. 1 boxes to Integer and 2.0 to Double, and T becomes their least upper bound, which is an intersection type: Number and Comparable of something, and on Java 12 and later also Constable and ConstantDesc.

I can use it as a Number, but I cannot write that type myself, and compareTo between elements compiles yet fails at run time. If I want List<Number>, I should declare it explicitly.

About this answer

Last updated
12 November 2026
Versions it names
Java 12, Java 5, Java 7, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which lines compile and what types are inferred?": https://scrivio-interview-library.netlify.app/java/generics/105ce0aabd/