Type inference is a compiler algorhytm that finds out omitted generic types by looking at the context. The algorhytm works out the most specific type that fits any possible valid arguments.
Here are some examples:
1) You can omit a parametrized type with the use of a diamon construct:
List<String> list = new ArrayList<>();
The full sentence would be:
List<String> list = new ArrayList<String>();
2) You can omit the parameter type:
Stack.push(10);
The full sentence would be:
Stack.<Integer>push(10);
3) You can infer the return type in the same way:
List<String> list = Container.getList();
The full sentence would be:
List<String> list = Container.<String>getList();
4) You can also infer the constructor parameter type:
GenericClass myObj = new GenericClass<>(5);
The full sentence would be:
GenericClass myObj = new GenericClass<Integer>(5);
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.