Java Reference
In-Depth Information
obj[0] = new Wrapper<String>("Hello"); // Array corruption
Long lv = nums[0].get(); // A ClassCastException
// Other code goes here
}
When you compile the above declaration of the process() method, you do not get an unchecked warning.
However, you get the following varargs warning because the compiler sees possible heap pollution when the varargs
parameter nums is assigned to the Object array obj :
warning: [varargs] Varargs method could cause heap pollution from non-reifiable varargs parameter
nums
Object[] obj = nums; // Heap pollution
^
1 warning
You can suppress the unchecked and varargs warnings for a varargs method with a non-reifiable type parameter
by using @java.lang.SuppressWarnings annotation as follows:
@SuppressWarnings({"unchecked", "varargs"})
public static void process(Wrapper<Long>...nums) {
// Code goes here
}
Note that when you use the @SuppressWarnings annotation with a varargs method, it suppresses warnings only at
the location of the method's declaration, not at the locations where the method is called.
Summary
Generics are the Java language features that allow you to declare types (classes and interfaces) that use type
parameters. Type parameters are specified when the generic type is used. The type when used with the actual type
parameter is known a parameterized type. When a generic type is used without specifying its type parameters, it is
called a raw type. For example, if Wrapper<T> is a generic class, Wrapper<String> is a parameterized type with String
as the actual type parameter and Wrapper as the raw type. Type parameters can also be specified for constructors and
methods. Generics allow you to write true polymorphic code in Java—code using a type parameter that works for all
types.
By default, a type parameter is unbounded, meaning that you can specify any type for the type parameter. For
example, if a class is declared with a type parameter < T> , you can specify any type available in Java, such as < String> ,
< Object> , < Person> , < Employee> , < Integer> , etc., as the actual type for T . Type parameters in a type declaration
can also be specified as having upper bounds or lower bounds. The declaration Wrapper<U extends Person> is an
example of specifying an upper bound for the type parameter U that specifies that U can be of a type that is Person or a
subtype of Person . The declaration Wrapper<? super Person> is an example of specifying a lower bound; it specifies
that the type parameter is the type Person type of a supertype of Person .
Java also lets you specify the wildcard, which is a question mark, as the actual type parameter. A wildcard as
the actual parameter means the actual type parameter is unknown; for example, Wrapper<?> means that the type
parameter T for the generic type Wrapper<T> is unknown.
The Java compiler attempts to infer the type of an expression using generics, depending on the context in which
the expression is used. If the compiler cannot infer the type, it generates a compile-time error and you will need to
specify the type explicitly.
 
Search WWH ::




Custom Search