September 2019
Intermediate to advanced
816 pages
18h 47m
English
In order to understand how LVTI can be combined with generic types, let's start with an example. The following method is a classical usage case of a generic type, T:
public static <T extends Number> T add(T t) { T temp = t; ... return temp;}
In this case, we can replace T with var and the code will work fine:
public static <T extends Number> T add(T t) { var temp = t; ... return temp;}
So, local variables that have generic types can take advantage of LVTI. Let's look at some other examples, first using the generic type, T:
public <T extends Number> T add(T t) { List<T> numberList = new ArrayList<T>(); numberList.add(t); numberList.add((T) Integer.valueOf(3)); numberList.add((T) Double.valueOf(3.9)); // error: ...