Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
What Is Type Argument Inference?
This section describes what is type argument inference - a process performed by the compiler to determine the actual type argument for a type parameter when invoking in a generic method.
What Is Type Argument Inference? Type argument inference is a process performed by the compiler to determine the actual type argument for a type parameter when invoking in a generic method.
Type argument inference also happens when the compiler tries to determine the actual type argument for a type parameter when instantiating an object from a generic class. See other parts of the book for more details.
Why Type Argument Inference Is Needed? Type argument inference is needed only if you do not specify type arguments explicitly when invoking a generic method.
When type arguments are specified as shown below in the type argument list, the compiler knows what the type argument is. It will not perform the type argument inference process:
// No, type argument inference is not needed Collections.<String>sort(myArrayList);
When type arguments are not specified as shown below, the compiler does not know what the type argument is. It will perform the type argument inference process to determine type arguments:
// Yes, type argument inference is needed Collections.sort(myArrayList);
Personally, I will always specify type arguments explicitly to avoid type argument inference, because compiler may infer to incorrect type arguments and lead to type incompatibility issues. But if you are a lazy programmer, you can take the risk and let the compiler to do the work for you using rules described below.
How Type Argument Inference Is Done? If you are not specifying type arguments when invoking generic methods, the compiler will perform the type argument inference process using these rules:
Next sections will show you some examples on how compiler does the type argument inference.
Last update: 2014.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
Generic Classes and Parameterized Types
►Generic Methods and Type Inference
Comparing Generic Method with Non-Generic Method
Non-Generic Method Example - maxNonGeneric()
Generic Method Example - maxGeneric()
Generic Methods in java.util.Collections Class
Testing Generic Methods in Collections Class
►What Is Type Argument Inference?
Type Argument Inference by Parameter List
Type Argument Inference by Return Value
Generic Methods using Parameterized Types
Parameterized Type as Generic Method Return Type
Lambda Expressions and Method References
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs