The document discusses generic type parameter naming conventions in Java like T, S, U, V, E, K, V, N and also discusses generic methods, wildcards in generics, upper and lower bounded wildcards.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views2 pages
Generic
The document discusses generic type parameter naming conventions in Java like T, S, U, V, E, K, V, N and also discusses generic methods, wildcards in generics, upper and lower bounded wildcards.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Generic type parameter naming convention
T − Type, and is mainly used to represent first generic type
parameter. S − Type, and is mainly used to represent second generic type parameter. U − Type, and is mainly used to represent third generic type parameter. V − Type, and is mainly used to represent fourth generic type parameter. E − Element, and is mainly used by Java Collections framework. K − Key, and is mainly used to represent parameter type of key of a map. V − Value, and is mainly used to represent parameter type of value of a map. N − Number, and is mainly used to represent numbers.
Generic Method Like the generic class, we can create a generic method that can accept any type of arguments. Here, the scope of arguments is limited to the method where it is declared. It allows static as well as non-static methods.
Wildcard in Java Generics
The ? (question mark) symbol represents the wildcard element. It means any type. If we write <? extends Number>, it means any child class of Number, e.g., Integer, Float, and double. Now we can call the method of Number class through any child class object.
We can use a wildcard as a type of a parameter, field, return type, or local
variable. However, it is not allowed to use a wildcard as a type argument for a generic method invocation, a generic class instance creation, or a supertype. Wildcards in Generics
The question mark (?) is known as the wildcard in generic programming. It
represents an unknown type. The wildcard can be used in a variety of situations such as the type of a parameter, field, or local variable; sometimes as a return type. Unlike arrays, different instantiations of a generic type are not compatible with each other, not even explicitly. This incompatibility may be softened by the wildcard if ? is used as an actual type parameter.
Types of wildcards in Java
1. Upper Bounded Wildcards:
These wildcards can be used when you want to relax the restrictions on a variable. For example, say you want to write a method that works on List < Integer >, List < Double >, and List < Number >, you can do this using an upper bounded wildcard. To declare an upper-bounded wildcard, use the wildcard character (‘?’), followed by the extends keyword, followed by its upper bound. 2. Lower Bounded Wildcards: It is expressed using the wildcard character (‘?’), followed by the super keyword, followed by its lower bound: <? super A>.