September 2019
Intermediate to advanced
816 pages
18h 47m
English
First, let's talk about LVTI and wildcards (?). It is a common practice to associate wildcards with Class and write something like this:
// explicit typeClass<?> clazz = Long.class;
In such cases, there is no problem with using var instead of Class<?>. Depending on the right-hand side type, the compiler will infer the correct type. In this example, the compiler will infer Class<Long>.
But notice that replacing wildcards with LVTI should be done carefully and that you should be aware of the consequences (or side effects). Let's look at an example where replacing a wildcard with var is a bad choice. Consider the following piece of code:
Collection<?> stuff = new ArrayList<>();stuff.add("hello"); // compile time errorstuff.add("world"); ...