Usage of Java generics <T> T, T, <T>

Let's talk about what is T in Java generics?

T is called a type variable in Java generics. So what is a type variable?

Type variables are used in the definition of the entire class to specify the return type of the method, and can also specify the types of fields and local variables. We can replace type variables with specific types to instantiate generic types.

If we often look at the source code, we can see many type variables similar to T. Let me briefly introduce what these type variables represent.

Ⅰ, T, U, S: represent any type

Ⅱ, K: Indicates the key type

Ⅲ, V: Indicates the value type

Ⅳ, E: Indicates the collection element type

Ⅴ, N: Indicates the digital type

After understanding the meaning of these types of variables, we return to the main story, what is the difference in usage of <T> T , T, and ?<T>

sample code

public class GenericMethod {
    
    
    public static void main(String[] args) {
    
    
        Double[] num = {
    
    1.11, 2.22, 3.33, 4.44, 5.55, 6.66};
        String[] str = {
    
    "Hello", "World", "你好", "世界"};
 
        Generic01 generic01 = new Generic01();
        generic01.toGeneric01(num);
        generic01.toGeneric01(str);
 
 
        Generic02<Double> doubleGeneric02 = new Generic02<>();
        doubleGeneric02.toGeneric02(num);
        Generic02<String> doubleGeneric03 = new Generic02<>();
        doubleGeneric03.toGeneric02(str);
    }
}
 
class Generic01 {
    
    
    public <T> T toGeneric01(T[] arr) {
    
    
        return arr[arr.length - 1];
    }
}
 
class Generic02<T> {
    
    
    public T toGeneric02(T[] arr) {
    
    
        return arr[arr.length - 1];
    }
}
 
class Generic03 {
    
    
    public <T> void toGeneric03(T[] arr) {
    
    
        T t = arr[arr.length - 1];
    }
}

In the sample test above, I wrote two classes Generic01 and Generic02 respectively. The return value style of the method written by the Generic01 class is <T> T, and the return value style of the method written by the Generic02 class is T. When writing, I found that if the return value of the method is written T, then this class must be generic. Simply put, it must be added after the Generic02 class <T>, otherwise an error will be reported!

After adding
insert image description here
the Generic02 class <T>as follows, when we call the method through this class, we must pass in the type, such as Integer, Double, String, our custom class, etc., and the Generic01 class returned by the method does not <T> Tneed If you know the type, just pass it in~

insert image description here
After talking about Generic01 and Generic02, let's talk about what Generic03 means?

The T in the Generic03 method
public <T>is the function of a modifier, which means it is a generic method, which means the same as static modification of a static method. However, here is not the return value type, but <T>means that the parameters passed in here can be generic or The variables defined below can be generic. <T>The purpose here is to ensure that the data type T can appear in the parameters or the variables defined in the method can be of Tthis data type.

Summarize T、 <T>、<T> T

T Represents
that only Tdata of type can be passed in, that is to say, the data type instantiated by the method is Tthe data type of . (According to the above code understanding, if Generic02, then it is limited Tto Double type.)

<T>Represents
the meaning of the modifier, which means that this is a generic method. The function is: the parameters that can be passed in are Tvariables of this type

<T> TRepresents
① The first one <T> represents generic type ② The second one represents the type of data Treturned (for example: Integer, Double, String, etc.) ③ The third one is to limit the incoming data type (for example: limited to arrays, collections, etc. wait)TT

Guess you like

Origin blog.csdn.net/qq_27480007/article/details/130932843