March 2019
Intermediate to advanced
242 pages
6h 21m
English
Java 7 introduced type inference for constructor arguments with generics. Consider the following line of code:
List<String> myThings = new ArrayList<String>();
In Java 7, the preceding line of code could be replaced with the following:
List<String> myThings = new ArrayList<>();
The preceding code shouldn't be confused with the following, which is trying to mix the generics with the raw types:
List<String> myThings = new ArrayList();
Java 7 also allowed type inference to invoke generic methods. For a generic method (say, print()) defined in a class (say, MyClass), the code would be as follows:
class MyClass<T> {
public <X> void print(X x) {
System.out.println(x.getClass());
}
}
The preceding code can be called ...
Read now
Unlock full access