I understand what Type Witness is as I see from Generics Trail In Java Documentation
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
Alternatively, if you omit the type witness,a Java compiler automatically infers (from the method's arguments) that the type parameter is Integer:
BoxDemo.addBox(Integer.valueOf(20), listOfIntegerBoxes);
Would like to understand
Some quick answers to your questions:
What is the right way to do this? Using Type Witness or let Java infer?
There is no technical correct answer for this as both approaches are valid. But code readability should always be the quality criterion. So the latter is better. Additionally you might change the type of your arguments at a later point in development. With type inference you do not have to change that line.
Is there a case where using type witness is absolutely needed?
Yes. It is needed when type cannot be inferred from the input arguments to a method. Maybe the generic type is only used for the return value, independently from the types of the arguments. Then you simply must specify it.
Is this a feature from Java 5 or added later?
Generics are a language feature from Java 5 on. Type inference is a compiler feature that is specified in the Java Language Specification (JLS). In the Java 8 JLS this topic got an own chapter. Each Java version did some enhancements in that feature. For example Java 7 introduced the diamond operator. Type witness for methods was already introduced in Java 5 as far as I know.
For completeness, this was added in Java 5. Here are the relevant parts of JLS Third Edition, which covers Java 5 and 6:
8.8.7.1 Explicit Constructor Invocations
ExplicitConstructorInvocation: NonWildTypeArgumentsopt this ( ArgumentListopt ) ; NonWildTypeArgumentsopt super ( ArgumentListopt ) ; Primary. NonWildTypeArgumentsopt super ( ArgumentListopt ) ; NonWildTypeArguments: < ReferenceTypeList > ReferenceTypeList: ReferenceType ReferenceTypeList , ReferenceType
15.12 Method Invocation Expressions
MethodInvocation: MethodName ( ArgumentListopt ) Primary . NonWildTypeArgumentsopt Identifier ( ArgumentListopt ) super . NonWildTypeArgumentsopt Identifier ( ArgumentListopt ) ClassName . super . NonWildTypeArgumentsopt Identifier ( ArgumentListopt ) TypeName . NonWildTypeArguments Identifier ( ArgumentListopt )
Note they are called NonWildTypeArguments
. The term "Type Witness" does not appear in the JLS. In JLS SE 8, the invocation specs are rewritten to use the pre-existing notion of TypeArguments
; and the word "witness" still appears nowhere.
(MethodName
already includes TypeName.Identifier
, so that fifth method invocation defines the explicit usage of a Type Witness, which is why it is not marked optional.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With