This is the fourth day of my participation in the November Gwen Challenge. See details: The last Gwen Challenge 2021.

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!

Foreword: yesterday we talked about the use of generics have a certain understanding! But generics can apply to methods as well as classes.

So, next we’ll look at the use of generics 2 – the learning of generic methods.

😒 Use of generics 2

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.

Today we will focus on learning how generics are used to define methods.

😲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:

    /** ** @param t The parameter passed into the generic * @param < t > The type of the generic * @return t The return value is of type 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"); / / incoming type String, return is also an Integer String type I = genericString. GenercMethod (123); // An Integer is passed in and an Integer 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.

🌸 summary

We believe that you have a better understanding of the use of generics and know how to use generics definition methods, so we look forward to the next chapter on the use of generics. Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

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!