Generic wildcard description

  • ? Represents an indeterminate Java type.
  • T stands for Java type.
  • K and V represent Key values in Java.
  • E is for Element.

What is the difference between Object and generic wildcards? Object is the root class of all classes. It is a concrete class. It may require type casting. , T, K, V, E, etc., the type is determined before the actual use, no casting is required.

T is a fixed generic type,? In other words, any class will do, because Object is the base class of all classes.

For example, Interge,String is <T extends Collection>,
Fixed generics mean that the type is fixed T; <? extends Collection>
Here? Represents an unknown typeHowever, the unknown type is actually a subclass of Collection, which is the upper bound of the wildcard.

Example:

  1. Specify entity classes for a particular paradigm

<T> T

Public static <T> T parseObject(String text, Class<T> clazz) {return parseObject(text, clazz); }Copy the code
  1. Specifies the entity class for a particular paradigm that contains a particular paradigm

<T> MessageInfo<T>

Public class MessageInfo<T> {private TeldHeader TeldHeader; private T Payload; } public static <T> MessageInfo<T> receiveMsgDto(Message message, T T = jsonObject.parseObject (dealMsgBody(message), CLS); TeldHeader = dealMsgHeader(message); return new MessageInfo<>(teldHeader,t); }Copy the code