An article on understanding Java generics

Bald bald bald bald 2022-02-13 07:08:29 阅读数:236

article understanding java generics

Generic (Generic) yes JDK5 One of the introduced Parameterized types features , yes Java The power of programming language . They improve the type safety of code , So that you can detect more errors at compile time . Generics increase code stability by detecting more errors at compile time .

 Insert picture description here

1. Why use generics

In short , Generics are defining classes , Interfaces and methods make types ( Classes and interfaces ) Become a parameter . Very similar to the more familiar formal parameters used in method declarations , Type parameters provide you with a way to reuse the same code with different inputs . The difference is that the input of formal parameters is value , The input of type parameter is type .

2. The benefits of generics

  • The code is more robust ( As long as the compiler has no warnings , Then the running period will not appear ClassCastException)
  • The code is simpler ( Don't force it )
  • The code is more flexible , Reuse

3. What does a generic look like

3.1. Generic interface

public interface Plate<T>{

public void set(T t);
public T get();
}

3.2. Generic classes

public class AIPlate<T> implements Plate<T>{

private List<T> items = new ArrayList<T>(10);
}

3.3. Generic methods

 public <T> AIPlate<T> getAIPlate(){

return new AIPlate<T>();
}

4. Generic erase

4.1. Java The principle of generics ? What is a generic erasure mechanism ?

Java The generics of are JDK5 New features , For downward compatibility , Virtual machines don't actually support generics , therefore Java It realizes a kind of Pseudo generics Mechanism , in other words Java Erase all generic information in the compiler , such Java There is no need to generate new types to bytecode , All generic types end up as primitive types , stay Java There is no generic information at runtime .

4.2. Java How does the compiler erase generics ?

  1. Check generic types , Get target type
  2. Erase type variables , And replace with qualified type
    If the type variable of a generic type is not qualified (< T >), Then use Object As a primitive type
    If there is a limit < T extends XClass>, Then use XClass As a primitive type
    If there are multiple restrictions < T extends XClass1 & XClass2>, Then use the first boundary XClass1 As primitive class
  3. Insert type conversions when necessary to keep types safe
  4. Build bridge methods to maintain polymorphism when extending

4.3. The impact of using generics and generic erasure ( side effect )

4.3.1. Generic type variables cannot use basic data types ?

Such as no ArrayList< int >, Only ArrayList< Integer >, When the type is erased ,ArrayList Type variables in the original type of < T > replaced Object, but Object Type cannot store int value .
 Insert picture description here

4.3.2. Out of commission instanceof Operator

Because after erasing ,ArrayList Only the original type , Generic information String Does not exist. , So I can't use it instanceof
 Insert picture description here

4.3.3. Problems with generics in static methods and static classes

Because the instantiation of generic parameters in generic classes is to define generic type objects ( such as ArrayList< Integer >) It's designated at the time of , Static members do not need to use objects to call , So no objects were created , How to determine what this generic parameter is .
 Insert picture description here

4.3.4. Method conflicts in generic types

Because the last two equals The method becomes the same .
 Insert picture description here

4.3.5. Cannot create generic instance

Because the type is uncertain .
 Insert picture description here

4.3.6. There is no generic array

Because arrays are covariant , After erasure, the principle of array covariance cannot be satisfied .
Covariance : for example A extends B, that A[ ] The parent class is B[ ].
 Insert picture description here

5. Java Generic PECS principle

  • If you just need to get the type from the collection T, Use <? extends T> wildcard
  • If you just need to type T Put it in the set , Use <? super T> wildcard
  • If you want to get and place elements , Then no wildcards are used .

PESC namely Producer extends Consumer super.

Unrestricted wildcards :AClass<?> Is an unknown generic type , Equivalent to AClass<? entends Object>
Qualifying wildcards :AClass<? extends T> upper bound ;AClass<? super T> Lower bound .

copyright:author[Bald bald bald bald],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130708270696.html