current position:Home>Deep understanding of generics
Deep understanding of generics
2022-05-15 07:50:34【User 7353950】
What is the point of introducing generics ?
- Generics are proposed to write reusability Better code .
- The essence of generics is parameterized type , That is to say, the data type being operated is Specify as a parameter .
Before introducing generics , Need to use Object To achieve universal 、 Different types of processing .
Drawbacks as follows :
- You need to cast to the desired type every time you use it .
- At compile time, the compiler doesn't know if the type conversion is OK , The runtime knows , unsafe .
In fact, the main goals of introducing generics are the following :
Type safety :
- The main goal of generics is to improve Java The type of program is safe
- At compile time, you can check out the cause of Java It's caused by the wrong type ClassCastException abnormal
- In line with the principle that the earlier the error, the less the cost
Eliminate casts :
- A side benefit of generics is , Get the target type directly , Eliminate many casts
- What you get is what you need , This makes the code more readable , And reduces the chance of error and potential performance benefits
Potential performance benefits :
- Because of the way generics are implemented , Supports generics ( almost ) Unwanted JVM Or class file change
- All the work is done in the compiler
- Compiler generated code does not use generics ( And cast ) The code you write is almost the same , It's just more type safe
The use of generics
The essence of generics is parameterized type , That is, the data type being manipulated is specified as a parameter .
This parameter type can be used in a class 、 Interface and method creation , Known as Generic classes 、 Generic interface 、 Generic methods .
Generic classes : The most common use of generic classes is as container classes that hold different types of data , such as Java Collection container class .
Generic interface : The implementation class needs to specify the specific parameter type when implementing the generic interface , Otherwise, the default type is Object type .
Generic methods : If the class is generic , Directly use the parameters declared by the class , If not , You need to declare the parameter type yourself .
Generic wildcard
<?>: Unlimited wildcards , It means that you can hold any type of .
<?> and <Object> Dissimilarity ,<?> Indicates an unknown type ,<Object> Represents any type of .
<? extends E>:
Use... In type parameters extends Indicates that the parameter in this generic must be E perhaps E Subclasses of , This has two advantages :
- If the type passed in is not E perhaps E Subclasses of , Editing failed .
- You can use... In generics E Methods , Otherwise, it has to be transformed into E Can be used .
<? super E>:
Use... In type parameters super Indicates that the parameter in this generic must be E perhaps E Parent class of .
Summary :
- Unlimited wildcards <?> and Object Somewhat similar , be used for Represents a scene with unlimited or uncertain scope .
- Two types of restricted general distribution < ? super E> and < ? extends E> It's also easy to confuse , Let's compare .
- Their purpose is to Make method interfaces more flexible , A wider range of types are acceptable .
- < ? super E> be used for Write or compare flexibly , Enables objects to write to containers of the parent type , So that the comparison method of parent type can be applied to subclass objects .
- < ? extends E> be used for Flexible reading , Make the method readable E or E Container object of any subtype of .
Basic principles of using wildcards :
- If the parameterized type represents a T The producers of , Use < ? extends T>;
- If it represents a T The consumer , Just use < ? super T>;
- If it is both production and consumption , Then there's no point in using wildcards , Because what you need is the exact parameter type .
- T The producer of means that the result will return T, This requires returning a specific type , There must be an upper limit to be specific ;
- T Consumers mean to operate T, This requires the container to be large enough , So the container needs to be T Parent class of , namely super T;
Generic type erasure
Written in generics Java Program and ordinary Java The procedure is basically the same , It's just More parameterized types and less type conversions .
In fact, generic programs are first transformed into general programs 、 Without generics Java It's handled after the program , The compiler is automatically completed from Generic Java To ordinary Java Translation ,Java The virtual machine runtime knows nothing about generics .
When compiler pairs with generics java When the code is compiled , It will carry out Type checking and type inference , Then generate normal bytecode without generics , This ordinary bytecode can be used by general Java The virtual machine receives and executes , This is called Type Erasure (type erasure).
All in all , A generic is a syntax sugar , It runs without storing any type of information .
The case of generics is called Immutability , The corresponding concept is covariance 、 Inversion :
- Covariance : If A yes B Parent class of , also A The container of ( such as List<A>) It's also B The container of (List<B>) Parent class of , It is called covariant ( Father son relationship is consistent ).
- Inversion : If A yes B Parent class of , however A The container of yes B Subclasses of containers , It is called inversion ( Put it in the container and usurp the throne ).
- immutable : Regardless of A B What does it matter ,A The container and B All containers have no parent-child relationship , Call it immutable .
Java The array in is covariant , Generics are immutable .
If you want a generic class to be covariant , It needs to be used The border .
- We know , The generic runtime is erased to the original type , This makes many operations impossible .
- If the boundary is not specified , The type parameter will be erased as Object.
- If we want the parameter to keep a boundary , You can set a boundary for the parameter , The generic parameter will be erased to its first boundary ( There can be multiple boundaries ), In this way, even after runtime erasure, there will be scope .
Generic rules
- The parameter type of a generic type can only be class ( Including custom classes ), Cannot be a simple type .
- The same generic type can correspond to multiple versions ( Because the parameter type is uncertain ), Different versions of generic class instances are incompatible .
- generic Type parameters can have multiple .
- Parameter types of generics have access to extends sentence , Used to be called “ Bounded type ”.
- Parameter types of generics It can also be a wildcard type , for example Class.
Generic usage scenarios
When the reference data type to be operated in the class is uncertain , Used to use Object To complete the expansion ,JDK 1.5 Post recommendation Use generics to complete extensions , At the same time, ensure safety .
Java in List<Object> And the original type List The difference between ?
The main difference between primitive types and parameterized types is :
- At compile time, the compiler does not perform type safety checks on the original types , But it will check the security of types with parameters .
- By using Object As a type , You can tell the compiler that the method can accept any type of object , such as String or Integer.
- You can pass any type with parameters to the original type List, But it can't put List<String> Pass it on to the receiver List<Object> Methods , Because of the immutability of generics , There will be compilation errors .
copyright notice
author[User 7353950],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102047501113.html
The sidebar is recommended
- Which securities company does qiniu school recommend? Is it safe to open an account
- Hyperstyle: complete face inversion using hypernetwork
- What activities are supported by the metauniverse to access reality at this stage?
- P2P swap OTC trading on qredo
- Google | coca: the contrast caption generator is the basic image text model
- SIGIR 2022 | Huawei reloop: self correcting training recommendation system
- Whether you want "melon seed face" or "national character face", the "face changing" technology of Zhejiang University video can be done with one click!
- Sorting of naacl2022 prompt related papers
- Servlet create project
- "Chinese version" Musk was overturned by the original: "if it's true, I want to see him"
guess what you like
[network security] web security trends and core defense mechanisms
[intensive reading] object detection series (10) FPN: introducing multi-scale with feature pyramid
007. ISCSI server chap bidirectional authentication configuration
2021-03-09
plot_ Importance multi classification, sorting mismatch, image value not displayed
[intensive reading] object detection series (XI) retinanet: the pinnacle of one stage detector
How to install MFS environment for ECS
[intensive reading] the beginning of object detection series (XII) cornernet: anchor free
Open source sharing -- a record of students passing through time
MOT:A Higher Order Metric for Evaluating Multi-object Tracking
Random recommended
- How to develop a distributed memory database (1)
- Reverse engineers reverse restore app and code, and localization is like this
- One line command teaches you how to export all the libraries in anaconda
- Bi tools are relatively big. Let's see which one is most suitable for you
- Read the history of database development
- Self cultivation of coder - batterymanager design
- Technology application of swift phantom type phantom in Apple source code learning
- Swiftui advanced skills: what is the use of the technology of swift phantom type phantom
- Swiftui advanced animation Encyclopedia of complex deformation animation is based on accelerate and vector arithmetic (tutorial includes source code)
- What problems remain unsolved in swiftui in 2022
- I'll set the route for fluent
- Flutter drawing process analysis and code practice
- Emoji language commonly used icon collection (interesting Emoji)
- 5.14 comprehensive case 2.0 - automatic induction door
- How to deploy redis service on k8s top?
- Importance of data warehouse specification
- Idea automatically generates serialization ID
- Why is it recommended not to use select * in MySQL?
- Let's talk about why redis needs to store two data structures for the same data type?
- Domain lateral move RDP delivery
- gDvuGqjmDS
- [learn slam orb_slam2 or slam3 from scratch] summary of all blog articles
- 20000 + star ultra lightweight OCR system pp-ocrv3 effect increased by 5% - 11%!
- A configurable canvas clock - Super multi style
- The pp-ocrv3 effect of 20000 + star ultra lightweight OCR system is further improved by 5% - 11%
- MySQL's golden rule: "don't use select *"
- True interview question: why does redis store a data type twice?
- High threshold for large factories? Five exclusive PDFs inside Alibaba will take you forward and win the offer
- Is it really hard to find a job? How on earth can I find a job with high salary without worrying about being laid off
- How to design knowledge center? (code attached)
- OWASP top 10 vulnerability analysis
- Are you still writing comment templates manually? Idea can generate annotation templates of classes and methods with one click. Click in if you don't know
- Numpy core syntax and code sorting summary!
- Can you believe that the swoole timer can realize millisecond task scheduling?
- Detailed explanation of art template engine
- Telephone subsystem of openharmony source code analysis -- call flow
- Yixin Huachen: how to do a good job in digital transformation in the power industry?
- One stop collaboration is really delicious - apipost
- Notes on modern algebra and series of questions: Chapter 1 (introduction of algebraic system)
- Notes on modern algebra and serialization of question types: Chapter 2 (properties of binary operation)