[introduction to Java] day14 a preliminary study on generics in Java

Generics is a very interesting and important concept. This article will briefly introduce the generics feature in Java, mainly from the following perspectives:

  1. What is generics.

  2. How to use generics.

  3. Benefits of generics.

1. What is generics?

2. How to use generics?

Let's first look at what generics look like:

This is the list interface. Here, e is used to replace the specific type, so that any type can be passed in. Maybe you have to ask, isn't it good to use object directly? Let's compare it with a chestnut:

First, implement the following in a non generic way:

This implements a wrapper class that can be used to access an object of any type. However, type conversion is required for each extraction. If the parameter type of the method is objholder, the exact type of the object stored in it cannot be known, which will bring a lot of unnecessary trouble.

Now let's look at how to implement with generics:

In this way, the generic parameter t is replaced by passing in type information such as string and integer. T here can be understood as a placeholder, and other letters can also be used. Once a specific type such as string is passed in, all places where t is used will be replaced with string type.

Compare the above two methods. What's the difference? For example, the implementation without generics is equivalent to a bag, which can contain any type of black box. You can put anything in it, but you may not know what you will take out next. The implementation of generics is equivalent to a black box with a label. Any information can be written on the label, such as fruit, Then this box can only contain fruit. You will also know that each time you take it out, it must be fruit rather than other things. Similarly, if you write miscellaneous grains, then this bag can only be used to contain miscellaneous grains, but in fact, it is the same bag, not one bag for each type of East and West. (because Java generics use the type erasure mechanism, I won't introduce what type erasure is for the time being, and I'll explain it in more detail in an article later).

Generics are widely used in container classes. For example, ArrayList < T > () represents arrays used to store specific types. In addition, there are many generic interfaces, such as comparable < T >. Using generics can bring great convenience.

Types can be restricted in generics. For example, < T extensions comparable < T > > means that only type objects that have implemented the comparable interface can be passed. Here, extensions is used instead of implementation, and only one can be written for the interface< T extensions number > indicates that only objects of class number or its subclasses can be received. On the contrary, the boundary wildcard is super. For example, < T super phone > means that only objects of type phone or its parent class can be received.

Special attention should be paid when using extensions and super because they have side effects, such as:

Because generics are designed for type safety, what if you go to list list are inserted, it is impossible to confirm what type it is. The compiler only knows that it is number type or its derived type, but cannot determine which specific type it is. The wildcard t indicates that all stored in it are of the same type. Therefore, if the lower boundary of extend is used, the save operation cannot be carried out. Similarly, the super lower boundary cannot be taken.

When should I use extends and when should I use super? Let's start with the conclusion:

3. What are the benefits of generics?   

Generics look cool, but at first glance, they seem useless? Sir, wait a minute. Come in and sit (funny).

The benefits of using generics are listed one by one:

1. Type safety.

This is most obvious. The main goal of generics is to improve the type safety of Java programs. By using the type restriction of variables defined by generics, it is easy to realize type detection during compilation and avoid a large number of unnecessary type errors caused by the use of object.

Without generics, these assumptions about the type of object variables only exist in the minds of programmers (or, if lucky, in code comments), and unsafe cast is required before each use.

2. Code reuse.

A great advantage of generics is that it increases the reusability of code. For example, the genericholder class above can access objects of any type without writing a wrapper class for each type.

3. Potential performance gains.

Generics make it possible for larger optimizations. In the initial implementation of generics, The compiler inserts cast (if there is no generics, the programmer will specify these cast) into the generated bytecode. However, the fact that more type information can be used in the compiler makes it possible to optimize future versions of the JVM. Due to the implementation method of generics, generics are supported (almost) no JVM or class file changes are required. All the work is done in the compiler, and the compiler generation is similar to no generics The code written during (and cast) is just to ensure type safety. The advantage of introducing generics into Java language is safety and simplicity. The advantage of generics is to check type safety during compilation, and all casts are automatic and implicit, so as to improve the reuse rate of code.

So far, the explanation of this article has been completed. If you want a better understanding, you still need to write more code and apply it in practice.

Welcome to continue to pay attention!

My blog will be synchronized to Tencent cloud + community, and I invite you to join me: https://cloud.tencent.com/developer/support-plan

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
分享
二维码
< <上一篇
下一篇>>