Java generics, understanding these is enough.
Contents of this document:
1、 What are Java generics?
2、 Common generic writing examples
Generic class definition:
Define generic methods:
Use:
definition:
use:
III. type erasure
Let's take a look at an example. The operate class is as follows:
After decompilating the corresponding class file, we use Java GUI to decompile Exe view the compiled code as follows
No, there is no < string > part at all. This generic type limited to string type has been "erased". When writing code, the generic type will be verified. If the type does not correspond, it cannot be added, but the generic type is removed after compilation.
4、 What do you want to use Java generics
After the introduction of "type erasure" in the third part above, it is easier to say why Java generics should be used. The generic type here is equivalent to "three rules". First, set the "rules" for you. My list < string > is used for operation
String type, you can't insert the person object. To put it bluntly, it's for type safety. So the benefits are:
Type safety: by knowing the type limitations of variables defined using generics, the compiler can verify type assumptions to a much higher extent. Without generics, these assumptions exist only in the minds of programmers (or, if lucky, in code comments).
Eliminate cast:
Knowing so much about the above is enough to use generics on a daily basis. Let's look at the PECS principles
5、 Pecs principles
Let's start with an example:
Here, PECS principles are as follows:
PriorityQueue of Java queue series
A diagram shows various queues in Java
[how does spring start? The starting process is described here in combination with the spring source code]( https://www.cnblogs.com/demingblog/p/7443714.html )[how spring MVC works, how spring MVC works]( https://www.cnblogs.com/demingblog/p/9925268.html )[spring exception handling. Analyze 400 exception handling process and solutions in combination with spring source code]( https://www.cnblogs.com/demingblog/p/9218271.html )
[how does the mybatis mapper interface find the implementation class - source code analysis]( https://www.cnblogs.com/demingblog/p/9544774.html )[HTTP server using netty]( https://www.cnblogs.com/demingblog/p/9970772.html )[netty implements heartbeat mechanism]( https://www.cnblogs.com/demingblog/p/9957143.html )[netty series]( https://www.cnblogs.com/demingblog/p/9912099.html )
[docker & k8s series I: getting started quickly]( https://www.cnblogs.com/demingblog/p/12905545.html )[docker & k8s Series II: local k8s environment construction]( https://www.cnblogs.com/demingblog/p/12905563.html )[docker & k8s series 3: deploy a single service instance in k8s]( https://www.cnblogs.com/demingblog/p/12905569.html )[docker & kubenetes Series IV: cluster, capacity expansion, upgrade, rollback]( https://www.cnblogs.com/demingblog/p/12919876.html )