Java generics (generic classes, generic methods, static methods, generic types, generic classes and generic method examples)

Posted by Mohammad on Wed, 08 Sep 2021 02:29:20 +0200

generic paradigm

Introduction to generics

  1. Generics can be understood as parameterized types, which mainly act on classes, methods and interfaces.
  2. Java generics and C + + templates: generics in Java are developed following the templates in C + +, so that developers can write general and flexible code.
  3. Pseudo generics: generics in Java are pseudo generics. After Java generics are developed, the information related to generics is eliminated in the compilation stage and will not be left to the runtime.

Generic type:

  • Generic method: the method has parameters, and the parameters of the method can be specified as some generic types;
  • Generic class: a class can also have parameters, and the type is passed into the class as a parameter;
  • Generic interface: parameter of the interface, which can also be generic;

When types are passed into generic methods, generic classes and generic interfaces, some types can be specified dynamically;
The role of generics:

  • Security check: in the compilation phase, you can check the code to bring fewer errors to the runtime;
  • Avoid forced rotation: avoid unnecessary safety problems caused by type of forced rotation;
  • Improve performance: using generics can improve the performance of Java;

Generic class

Generic class: if a declared generic type is used after the class name, the generic type T type can be used in this class; In particular, the following two methods in this class are not generic methods; The return value type of the parameter is t, but this t is used as a normal type, not a generic type declared in the method;
If the class, interface and method are generic classes, generic interfaces and generic methods, the class, interface and method must be decorated by a T with angle brackets;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

Complete generic class

/**
 * Generic class
 *  This T type is used as a parameter
 *  T Is a parameterized type that can be passed in externally
 *
 * @param <T>
 */
public class Student<T> {

    private String name;
    private int age;
    /**
     * The data type is unknown
     *  Using a generic representation, the runtime determines the type
     */
    private T data;

    public Student(String name, int age, T data) {
        this.name = name;
        this.age = age;
        this.data = data;
    }

    /**
     * The method is not generic
     *  This method is a normal method, and the return value type is T type
     * @return
     */
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

generic method

Generic methods: generic methods are used to declare generic methods before the return value of methods; Pass a type as a parameter;
Number of generics: this method is a generic method, and 2 generics are specified. There can be many generics. Multiple generics are separated by commas;
Generic methods and generic types in generic classes:
Different generics: the generic T specified by the generic method has no relationship with the generic T in the class, and the two T can be different types;
The generic type is the same: the generic type T defined in the generic method is the same type as the parameter type T, the return value type T, and the internal t of the method;

/**
 * Generic class
 *  This T type is used as a parameter
 *  T Is a parameterized type that can be passed in externally
 *
 * @param <T>
 */
public class Student<T> {

    private String name;
    private int age;
    /**
     * The data type is unknown
     *  Using a generic representation, the runtime determines the type
     */
    private T data;

    public Student(String name, int age, T data) {
        this.name = name;
        this.age = age;
        this.data = data;
    }

    /**
     * Generic method is to pass a type as a parameter
     *      Method specifies a generic type, which is written as follows
     *
     * The method is generic
     *      Method specifies 2 generics
     *      Number of generics. There can be many generics
     *      Multiple generics are separated by commas
     *
     * The generic T specified for the method has nothing to do with the generic T in the class
     *      The two T's can be of different types
     *
     * Generic T defined in generic method
     *      T with parameter type
     *      T of return value type
     *      Method internal T
     *      All of the same type
     *
     * It has nothing to do with T in generic classes
     *
     * @param <T>
     * @param <A>
     * @return
     */
    public <T, A> T getData2(T arg){
        T data = arg;
        return data;
    }
}

Generics of static methods

Static method generic type: if the generic type T in the class is used as a parameter or return value in the static method, this use is wrong;
If a generic T must be used in a static method, the generic T must be a generic of a static method, not a generic of a class;
Incorrect usage:
Correct usage:

Complete examples of generic classes and generic methods

/**
 * Generic class
 *  This T type is used as a parameter
 *  T Is a parameterized type that can be passed in externally
 *
 * @param <T>
 */
public class Student<T> {

    private String name;
    private int age;
    /**
     * The data type is unknown
     *  Using a generic representation, the runtime determines the type
     */
    private T data;

    public Student(String name, int age, T data) {
        this.name = name;
        this.age = age;
        this.data = data;
    }

    /**
     * The method is not generic
     *  This method is a normal method, and the return value type is T type
     * @return
     */
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * Generic method is to pass a type as a parameter
     *      Method specifies a generic type, which is written as follows:;
     *
     * The method is generic
     *      Method specifies 2 generics
     *      Number of generics. There can be many generics
     *      Multiple generics are separated by commas
     *
     * The generic T specified by the generic method has nothing to do with the generic T in the class
     *      The two T's can be of different types
     *
     * Generic T defined in generic method
     *      T with parameter type
     *      T of return value type
     *      Method internal T
     *      All of the same type
     *
     * It has nothing to do with T in generic classes
     *
     * @param <T>
     * @param <A>
     * @return
     */
    public <T, A> T getData2(T arg){
        T data = arg;
        return data;
    }

    /**
     * If you use generics in a class in a static method
     *      This use is wrong
     *
     * If you must use generic T in static methods
     *      Then the generic T must be a generic of a static method
     *      Cannot be a generic type of a class
     *
     * @param arg
     * @return
     */
    public static <T> T getData3(T arg){
        T data = arg;
        return data;
    }
}

Topics: Java