Type
Inference with Generics
Beginning with JDK 7, it is
possible to shorten the syntax used to create an instance of a generic type. To
begin, consider the following generic class:
class MyClass<T, V> { T ob1;
V ob2;
MyClass(T o1, V o2) { ob1 = o1;
ob2 = o2;
}
// ...
}
Prior to JDK 7, to create an
instance of MyClass, you would have
needed to use a statement similar to the following:
MyClass<Integer, String> mcOb =
new MyClass<Integer, String>(98, "A
String");
Here, the type arguments
(which are Integer and String) are specified twice: first,
when mcOb is declared, and second,
when a MyClass instance is created
via new. Since generics were introduced by JDK 5, this is the
form required by all versions of Java prior to JDK 7. Although there is nothing
wrong, per se, with this form, it is a bit more verbose than it needs to be. In
the new clause, the type of the type
arguments can be readily inferred from the type of mcOb; therefore, there is really no reason that they need to be
specified a second time. To address this situation, JDK 7 added a syntactic
element that lets you avoid the second specification.
Today the preceding
declaration can be rewritten as shown here:
MyClass<Integer, String> mcOb = new
MyClass<>(98, "A String");
Notice that the instance
creation portion simply uses <>, which is an empty type argument list.
This is referred to as the diamond
operator. It tells the compiler to infer the type arguments needed by the
constructor in the new expression.
The principal advantage of this type-inference syntax is that it shortens what
are sometimes quite long declaration statements.
The preceding can be
generalized. When type inference is used, the declaration syntax for a generic
reference and instance creation has this general form:
class-name<type-arg-list > var-name = new class-name <>(cons-arg-list);
Here, the type argument list
of the constructor in the new clause
is empty.
Type inference can also be
applied to parameter passing. For example, if the following method is added to MyClass,
boolean isSame(MyClass<T, V> o) {
if(ob1 == o.ob1 && ob2 == o.ob2) return
true; else return false;
}
then the following call is
legal:
if(mcOb.isSame(new MyClass<>(1,
"test")))
System.out.println("Same");
In this case, the type
arguments for the argument passed to isSame(
) can be inferred from the parameter’s type.
Because the type-inference
syntax was added by JDK 7 and won’t work with older compilers, most of the
examples in this book will continue to use the full syntax when declaring
instances of generic classes. This way, the examples will work with any Java
compiler that supports generics. Using the full-length syntax also makes it
very clear precisely what is being created, which is important in example code
shown in a book. However, in your own code, the use of the type-inference
syntax will streamline your declarations.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.