The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

The generic

1. Why generics

The early Object type could accept any Object type, but in practical use, there were conversion problems. That’s why Java provides generics to solve this security problem.

  • Take a look at a classic case:

    public static void main(String[] args) {
            // Test the classic case of generics
            ArrayList arrayList = new ArrayList();
            arrayList.add("helloWorld");
            arrayList.add("taiziyenezha");
            arrayList.add(88);// Since the collection is not qualified, any type can be stored in it
    
            for (int i = 0; i < arrayList.size(); i++) {
                // Requirements: To print the length of each String, convert the object to StringString str = (String) arrayList.get(i); System.out.println(str.length()); }}Copy the code

    When I run this code, the program runs with an exception:

    Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    Copy the code

    A cast exception occurred. Why?

    Because an ArrayList can hold any type of element. In this example, we add a String type and an Integer type, and then use them as String, causing a ClassCastException when cast to String.

    This is obviously not what we expect, and if the program has a potential bug, we expect to be told about the bug at compile time rather than reporting an exception at run time. Generics were created after jdk1.5 to solve problems like this, which can be solved at compile time. This allows you to specify that classes or methods support generics when designing your API, so that we can use the API more succinctly and get compile-time syntax checks.

    If we change the first line of code that declares the ArrayList initialized, the compiler will find problems like this at compile time. Now let’s see what happens.

    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("helloWorld");
    arrayList.add("taiziyenezha");
    arrayList.add(88);// At compile time, the compiler will report an error
    Copy the code

    This will avoid exceptions when we type strongly.

2. What are generics

  • Generics: A special type that postpones specifying a type until object creation or method invocation. That is, in the use of generics, the data type of the operation is specified as a parameter, and this parameter type can be used in classes, methods, and interfaces, called generic classes, generic methods, and generic interfaces respectively.

    Note: In general, when creating an object, the specific type is determined from an unknown type. When no generics are specified, the default type is Object.

3. Benefits of using generics

  • Avoid the trouble of strong type rotation.
  • It provides compile-time type safety, ensuring that only objects of the correct type can be used on generic types (usually collections of generic types), and avoiding classcastExceptions at runtime.

4. Use of generics

Generics are often used heavily in collections, but we can also learn about generics in their entirety. Generics can be used in three ways: generic classes, generic methods, and generic interfaces. Pass the data type as a parameter.

4.1 a generic class

Generic types are used in the definition of classes and are called generic classes. Generics make it possible to open the same interface to operations on a set of classes. The most typical are the collection framework container classes: List, Set, Map.

  • Generic class definition format:

    The modifierclassClass name < variables representing generics >{}Copy the code

    In case you’re not sure how to use it, I made a simple generic class:

    / * * *@param<T> Here is an illustration of the T in <T> : * Where T can be written as an arbitrary identifier, common arguments such as T and E denote generics * Generics are not concrete when defined, but become concrete when used. * Determine the specific data type of the generic at the time of use. That is, the generic type is determined when the object is created. * /
    public class GenericsClassDemo<T> {
        //t This member variable is of type t, which is externally specified
        private T t;
    
        // The generic constructor parameter t is also of type T, which is externally specified
        public GenericsClassDemo(T t) {
            this.t = t;
        }
    
        // The generic method getT returns a value of type T, whose type is externally specified
        public T getT(a) {
            returnt; }}Copy the code

    Generics are not concrete when they are defined, but concrete when they are used. Determine the specific data type of the generic at the time of use. That is, determine the generic type when creating an object.

  • For example: Generic

    genericString = new Generic

    (“helloGenerics”);

    In this case, the generic identifier T is of type String, so we can think of the class we wrote earlier as follows:

    public class GenericsClassDemo<String> {
        private String t;
    
        public GenericsClassDemo(String t) {
            this.t = t;
        }
    
        public String getT(a) {
            returnt; }}Copy the code

    This is also handy when your generic type wants to change to an Integer type. T = T; T = T; T = T;

    Generic<Integer> genericInteger = new Generic<Integer>(666);

  • Note: to define a generic class, do WE have to pass in a generic type argument?

    This is not the case. When you use generics, if you pass in a generic argument, the generic argument will be restricted accordingly, and the generics will do what they are supposed to do. A method or member variable defined using a generic type in a generic class can be of any type, provided that no generic type argument is passed. That is, as in the classic case before, there is no generic type to write ArrayList, which is prone to type overcasts.

4.2 Generic methods

A generic method specifies the specific type of the generic when calling the method.

  • Definition format:

    Modifier < variables representing generics > return value type method name (parameter){}Copy the code

    Such as:

    / * * * *@paramT passes the parameter * to the generic@param<T> The type of the generic *@return<T> <T> <T> <T> <T> * 2) Only methods that declare <T> are generic, and member methods that use generics in a generic class are not generic. * 3) <T> indicates that the method will use the generic type T before you can use the generic type T in the method. * 4) Like the definition of generic classes, T can be written as any identifier, and common parameters such as T and E are often used to represent generics. * /
        public <T> T genercMethod(T t){
            System.out.println(t.getClass());
            System.out.println(t);
            return t;
        }
    Copy the code
  • When calling a method, determine the type of the generic type

    public static void main(String[] args) {
        GenericsClassDemo<String> genericString  = new GenericsClassDemo("helloGeneric"); // The generics here can be different from the generic methods called below.
    
        String str = genericString.genercMethod("hello");// String is passed in, and String is returned
        Integer i = genericString.genercMethod(123);// An Integer type is passed in and an Integer type is returned
    }
    Copy the code

    Here we can see the results:

    class java.lang.String
    hello
    class java.lang.Integer
    123
    Copy the code

    As you can see, the generic method gets a different type depending on the type of argument we pass in. Generic methods allow methods to change independently of classes.

4.3 Generic Interfaces

Generic interfaces are defined and used in much the same way as generic classes. Generic interfaces are often used in producers of various classes.

  • Define the format

    The modifierinterfaceInterface name < variables representing generics >{}Copy the code

    Look at the following example to see how to define a generic interface:

    /** * define a generic interface */
    public interface GenericsInteface<T> {
        public abstract void add(T t); 
    }
    Copy the code
  • Use the format

    • Define the type of the generic when defining the class

      public class GenericsImp implements GenericsInteface<String> {
          @Override
          public void add(String s) {
              System.out.println("Set generics to String"); }}Copy the code
    • 2. The type of the generic is never determined until the object is created

      public class GenericsImp<T> implements GenericsInteface<T> {
          @Override
          public void add(T t) {
              System.out.println("No set type"); }}Copy the code

      Determine generics:

      public class GenericsTest {
          public static void main(String[] args) {
              GenericsImp<Integer> gi = new GenericsImp<>();
              gi.add(66); }}Copy the code

5. Generic wildcards

When using generic classes or interfaces, you can pass data in which the generic type is not determined by the wildcard <? > said. But once you use generic wildcards, you can only use the generic methods of the Object class, not the elements in the collection themselves.

5.1 Wildcard Basic Usage

Generic wildcards: can be used when you do not know what type to use for receiving? ,? Indicates an unknown wildcard.

At this point, only data can be accepted, and data cannot be stored in the collection.

Here’s an example you can understand and use:

/ /? Indicates that any type can be received
// There is no inheritance, polymorphism, the left and right sides of the generic should be the same
//ArrayList list = new ArrayList
       
        (); This is wrong
       

// Generic wildcard? : Write 
      ArrayList<? > list1 =newArrayList<Object>(); ArrayList<? > list2 =newArrayList<String>(); ArrayList<? > list3 =new ArrayList<Integer>();

Copy the code

Note: generics do not have inheritance, polymorphism, the left and right sides of the generic should be the same, after jdk1.7, the right side of the generic can be omitted

And generic wildcards? The generics on the right can be of any type.

Generic wildcards? Mainly used in parameter passing, let’s have a look at it:

public static void main(String[] args) {
    ArrayList<Integer> list1 = new ArrayList<Integer>();
    test(list1);
    ArrayList<String> list2 = new ArrayList<String>();
    test(list2);
}

public static void test(ArrayList
        coll){}Copy the code

Hey hey, is not to see the fierce wildcard, can pass different similar into the method!

5.2 Advanced Use of WildCards

When you set generics, you can actually set them as long as they’re classes. But in JAVA generics you can specify the upper and lower limits of a generics.

Upper limits on generics:

  • format:Type name <? Extends class > Object name
  • meaning:Only this type and its subclasses can be accepted

The lower limit of generics:

  • Format: Type name
    object name

  • Meaning: Only this type and its parent type can be accepted

For example, we now know the class Object, Animal, Dog and Cat, of which Animal is the parent of Dog and Cat

class Animal{}/ / parent class

class Dog extends Animal{}/ / subclass

class Cat extends Animal{}/ / subclass
Copy the code
  • First let’s look at the upper bound of generics
    :

    // ArrayList
             list = new ArrayList(); / / an error
            ArrayList<? extends Animal> list2 = new ArrayList<Animal>();
            ArrayList<? extends Animal> list3 = new ArrayList<Dog>();
            ArrayList<? extends Animal> list4 = new ArrayList<Cat>();
    Copy the code

    As you can see, the upper limit of a generic type can only be the type of that type and its subclasses.

  • Let’s look at the lower bound of generics
    :

            ArrayList<? super Animal> list5 = new ArrayList<Object>();
            ArrayList<? super Animal> list6 = new ArrayList<Animal>();
    // ArrayList
             list7 = new ArrayList
            
             (); / / an error
            
    // ArrayList
             list8 = new ArrayList
            
             (); / / an error
            
    Copy the code

    As you can see, the lower limit of a generic can only be the type of that type and its parent class.

  • The upper and lower bounds of generic types are also used for passing parameters:

    For example: now we know the Object class, String class, Number class, Integer class, where Number is the parent of Integer class

public static void main(String[] args) {
    Collection<Integer> list1 = new ArrayList<Integer>();
    Collection<String> list2 = new ArrayList<String>();
    Collection<Number> list3 = new ArrayList<Number>();
    Collection<Object> list4 = new ArrayList<Object>();
    
    getElement(list1);
    getElement(list2);/ / an error
    getElement(list3);
    getElement(list4);/ / an error
  
    getElement2(list1);/ / an error
    getElement2(list2);/ / an error
    getElement2(list3);
    getElement2(list4);
  
}
// The upper limit of generics: the generics at this time? Must be type Number or a subclass of type Number
public static void getElement1(Collection<? extends Number> coll){}
// The lower limit of generics: the generics at this time? Must be type Number or a parent of type Number
public static void getElement2(Collection<? super Number> coll){}
Copy the code

We are done with generics!

6. Wrap up the loose flower

I believe you all have a certain understanding of generics, in peacetime development, more common use in the use of generic framework in the collection of List and Map. There are a lot of applications, look forward to you slowly discover!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!

Note: If there are any mistakes and suggestions, please leave a message! If this article is helpful to you, I hope you will pay attention to it for three times. Thank you very much! You can also search the wechat prince Nezha public number private chat me, thank you guys!