Generic
Generic
Using ‘Generic’, problems that may be used in the wrong type may be eliminated during the compiling. Generic is used widely in ‘Collection’, ‘Lambda expression’, ‘Stream’, and ‘NIO’. Type can be used as a parameter when defining Class, Interface, and Method. *Type parameter: When writing code, it is replaced with a specific type to generate various code.
Advantage
-
JAVA compiler makes a **strong type check** on generic codes to eliminate problems caused by wrong used types.
-
Eliminate unnecessary casting.
list = new ArrayList(); //Non-generic
list.add("Hello, world");
String str = (String) list.get(0);
List<String> list = new ArrayList<String>(); //Generic
list.add("Hello, world");
String str = list.get(0);
- Code becomes more reusable.
Generic type
It refers to class and interface with type as parameter. “< >” is attached after the class or interface name and the type parameter is located between them. Type parameters are not predetermined, so it is okay to designate them arbitrarily.
Type | Explain |
---|---|
T | Type |
E | Element |
K | Key |
V | Value |
N | Number |
public class className <T> {
...
}
public interface interfaceName <T> {
...
}
Type parameters can be made with the same rule as the variable name, but are generally expressed in one letter of alphabetic capital. ‘Object’ containing all object types may be used, but casting is required when saving and casting is aslo required when reading.
It is not necessary to use only one type parameter. Two or more type parameters may be used, which are referred to a ‘multi-type parameters’. Each type parameter is divided into ‘,’.
//Before JAVA6
Product<TV, String> product = new Product<TV, String>();
//After JAVA7
Product <TV, String> product = new Product<>();
Generic type also could be a parent class like other types.
public class ChildClass<T, M> extends Class<T, M> {
...
}
// Child generic type may have additional type parameter.
public class ChildClass<T, M, C> extends<T, M> {
...
}
Generic Method (<T, R> R method (T t))
The generic method is a method with type parameters in parameter type and return type.
How to Declare
- add “< >” in front of the return type and describe the type parameter.
- Use the return type and type parameter.
public <type parameter, ...> returnType methodName (parameter, ...) {
...
}
How to Call
- Explicitly designating a specific type of type parameter in code.
- It is possible to allow the compier to estimate a specific type by looking at the type of the mediating value.
returnType variance = <specific type> methodName (medium);
returnType variance = methodName (medium);
Bounded type parameter
It is often necessary to specify a specific type specified in the type parameter. A generic method, for example, that calcuates numbers should have only instances of Number type or subclass type (Byte, Short, Integar, Long, Double) as a medium.
How to Declare
Put the keyword ‘extends’ after the type parameter, and specify the upper type. *The upper type may be possible not only class also interface. Do not use 'implements' even interface.
public <T extends upperType> returnType methodName (medium, ...) {
...
}
The specific type specified in the type parameter possible an upper type or only a lower or an implementation class of an upper type. Note that what can be used as a type parameter variable within ‘{ }’ of the method is limited to upper type members(fields, methods). Fields and methods that exist only in lower types cannot be used.
public <T extends Number> int compare(T t1, T t2) {
double v1 = t1.doubleValue(); // doubleValue() of Number
double v2 = t2.doubleValue();
return Double.compare(v1, v2);
}
Wildcard Type (<?>, <? extends …>, <? super …>)
When using the generic type as a medium or return type, a wildcard could be used instead of a specific type.
Category
-
Generic type<?>: Unbounded Wildcard (No restrictions) It is used only when methods that do not depend on type parameters are used or when the functions provided by the Object method are sufficient. All classes or interface types could come as a specific type that replaces the type parameter. Internally it is defined and used as an Object, and all types can be recived as factors.
-
Generic type<? extends upperType>: Upper Bounded Wildcards (Upper class restrictions) Only child classes of specific class are received as factors, and mainly used to alleviate the limitation of variables. As specific type that replaces the type parameter, only the lower type may come.
-
Generic type<? super lowerType>: Lower Bounded Wildcards (Lower class restrictions) Only parent classes of specific class are received as factors, and as specific type that replaces the type parameter, only the upper type may come.
Reference
Enjoy Reading This Article?
Here are some more articles you might like to read next: