“This is the second day of my participation in the November Gwen Challenge. See details of the event: 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 why we need to use generics, but we don’t know much about the definition and use of generics.

So today we’re going to take a closer look at generics.

😜 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.

The main purpose of implementing generics in Java is to allow exception bugs to be found early in the programming source code, rather than at runtime. If we can find exception bugs at compile time, we can save programmers a lot of time debugging Java programs.

Compilation errors can therefore be found and fixed relatively easily and quickly. Also, the fact that generics only exist at compile time is one of the most important facts to keep in mind as we learn about Java generics.

😝 Benefits of using generics

Like the classic example we showed yesterday, without generics, what would the set end up like,

  • Collection is an operation that has no restrictions on the type of the element. My ArrayList collection was originally loaded with all strings, but I stored Integer numbers in the collection without any syntax errors.
  • If you throw any Object into a collection, the collection doesn’t know what the element’s type is, just Object. So when WE get(), we return Object. And to get that object, we need to cast it

With generics:

  • The code is much cleaner without casting
  • As long as there are no warnings at compile time, there will be no ClasscastExceptions at run time
  • Readability and stability define the type when you write a collection

So the benefits are:

  • 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.

🌸 summary

I believe that you have a deeper understanding of generics, know the concept of generics, and the benefits of using it, we continue to look forward to the next chapter of 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!