Comprehensive summary of Java generic knowledge points

preface

I have always believed that generics is a very basic and important concept in programming language design. What are generics in Java? Why are they there? How did they develop? When learning the basic knowledge, a thorough understanding of generic drugs is very important. Therefore, I read the general chapter of Java programming ideas. Unfortunately, I don't have much experience. I've seen it several times. We can only have the opportunity to learn in the future. But I also understand a lot. The following is my understanding of generic drugs. If there are any mistakes, I would like to point out.

concept

Source: Java was not generic until it was introduced in JDK 1.5. Java generics are implemented by erasure. Do you know what erasure is? Look down.

Concept: general classes and methods can only use specific types; Basic type or custom class. If you want to write code that can be applied to multiple types of code, this strict restriction will impose great constraints on the code. Generics implement the concept of parameterized types, enabling code to be applied to multiple types. When generics appear in programming languages, their initial goal is to have a wide range of expressive capabilities for classes and methods.

Defining Simple Generics

There are many reasons for generics, one of the most important is to create container classes. We do not specify the type for the time being, but decide what type to use in the future. To achieve this, you need to use type parameters after the class name, enclosed in angle brackets. Then, when this class is used, the parameters of this type are replaced with the actual type. In the following example, t is a type parameter. The code is as follows:

However, in many source code, some generic classes have multiple generic parameters, such as Java. Util function. Dual function, with three types of parameters T, u and r.

generic method

Generic methods make methods independent of classes. When writing common code, the basic guiding principle is to use common methods as much as possible. This means that if you can use generic methods instead of generic methods for an entire class, you can use generic methods because they can make things clearer. In addition, for static methods, the type parameters of generic classes cannot be accessed, so if static methods need to use generalization functions, they must be turned into generic methods.

Generic erasure

When I read the "deleted mysteries" section of the general chapter of Java programming ideas, I was particularly dizzy, and then when I looked down, I became more and more confused. Especially when you look at boundaries and wildcards, it's a little confusing. First look at what erasure is. Information about generic parameter types is not available in generic code. Java generics are implemented by erasure, which means that when you use generics, any particular type will be erased, and the only thing you know is to use objects. Since Java did not initially introduce generics, it was designed to be compatible with older versions of the JDK. Eracess is a compromise between Java's common implementation. Therefore, when you run, list is the same as list. Note that it is running, but at compile time, list represents the list container of this string type and list represents the list container of the current integer type. For example, examples from Java programming ideas.

Boundary of generics

A generic type in Java. When compiled, T represents a type. If no boundary is specified, it is equal to an object. We can use the extends keyword to specify boundaries for generics. In order to call f (), we can help the generic class given the boundary of the generic class and tell the compiler that the type behind the boundary must be accepted. The extension keyword is used here. Change the above code to

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>