A list,

  • Generics: a feature introduced in JDK5 that provides compile-time type safety checks that allow illegal types to be detected at compile time. The essence of this is parameterized typing, which means that the data type being operated on is specified as a parameter.

  • When it comes to parameters, you’re most familiar with defining tangible arguments to a method and then passing the arguments when the method is called. So what about parameterized types? As the name implies, you parameterize the type from the original concrete type, and then pass in the concrete type when you use/call it.

  • This parameter type can be used in classes, methods, and interfaces, known as generic classes generic methods, and generic interfaces, respectively.

  • The genericDefinition format:

    • < type > : Specifies the format of a type, where the type can be considered as a parameter.

    • < Type 1, type 2… > : Specifies the format of multiple types separated by commas. The types here can be treated as parameters.

    • A given type can be treated as an argument in a future call, and the type of the argument can only be a reference data type.

  • The genericThe benefits of:

    • Bring run-time problems forward to compile time

    • Casting is avoided

2. Generic classes

  • Generic class definition format:

    • Format: modifier class class name < type > {}

    • Public class Generic

      {}

    • Here T can be written as any identifier, and common parameters such as T, E, K, and V are often used to represent generics.

  • Define a common Generic class generic.java

    Public class Generic<T> {// Define private attributes private T T; Public T getT() {return T; } public void setT(T T) {this.t = T; }}Copy the code
  • Used in the main function to specify that the generic type is String. When using generic classes, all generic types refer to String.

    Public class test {public static void main(String[] args) {public static void main(String[] args) {new Generic<String> g1 = new Generic<String>(); // use set method g1.sett (" DZM "); }}Copy the code

Generic methods

  • Generic method definition format:

    • Format: modifier < type > Return value Type method name (type quantity name){}

    • Public

      void show(T T){}

  • Case study:

    Public class test {public static void main(String[] args) {// use generic DZMLog(" DZM "); DZMLog(88); DZMLog(true); Static <T> void DZMLog(T T) {system.out.println (T); // Static <T> void DZMLog(T T) {system.out.println (T); }}Copy the code

    Output:

    dzm
    88
    true
    Copy the code

Generic interface protocols

  • Generic interface protocol definition format:

    • Interface Interface name < type >{}

    • Public interface Generic

      {}

  • Define a generic.java interface protocol

    Public interface Generic<T> {public default void show(T T) {system.out.println (T); }}Copy the code
  • Define a Genericpro. Java class to comply with the generic. Java interface protocol

    Public class GenericPro<T> implements Generic<T> {Override public void show(T T) {// implements Generic<T>Copy the code

5. Type wildcard

  • To represent the parent classes of various generic lists, you can use type wildcards

  • Type wildcard definition format:

    • Type wildcard:

    • List
      : represents a List of unknown elements whose elements can match any type.

    • This wildcard List simply means that it is a parent of the various generic lists, and you cannot add elements to them.

  • Upper and lower limits of wildcards:

    • If we don’t want List
      is the parent of any generic List. You only want it to represent the parent of a certain class of generic List. You can use the upper bound of type wildcards.

    • Type wildcard – upper limit:

    • Sample List
      : This represents a type of Number or a subtype, and must not be a parent.

    • Type wildcard – Lower limit:

    • Sample List
      : indicates a type of Number or its parent type, but must not be a subtype.

  • Case study:

    import java.util.List; import java.util.ArrayList; Public class test {public static void main(String[] args) {// type wildcard: <? > List<? > list1 = new ArrayList<String>(); List<? > list2 = new ArrayList<Number>(); List<? > list3 = new ArrayList<Integer>(); // Type wildcard line: <? Extends type > List<? extends Number> list5 = new ArrayList<String>(); // This is an error because String does not belong to Number or its subtype List<? extends Number> list6 = new ArrayList<Number>(); List<? extends Number> list7 = new ArrayList<Integer>(); // Type wildcard offline: <? Super type > List<? super Number> list5 = new ArrayList<String>(); String does not belong to Number or its parent type List<? super Number> list6 = new ArrayList<Number>(); List<? super Number> list7 = new ArrayList<Integer>(); // This is an error because Integer is not Number or its parent type}}Copy the code