Simple understanding of generics [java]

Table of contents

1.What are generics

 2.Introduce generics

2.1 Grammar 

3. Use of generic classes

3.1 Grammar

4 How are generics compiled?

4.1 Erase mechanism

5. Upper and lower bounds of generics

5.1 Grammar

 6.Attention


1.What are generics

        General classes and methods can only use specific types: Either a basic type or a custom class. If you want to write code that can be applied to multiple types, this rigid restriction will be very restrictive to the code. ------- Source "JavaProgramming Thoughts" is an introduction to generics.      

        Generics are a new syntax introduced in JDK1.5. In layman’s terms, generics: is applicable to many types . From a code perspective, the type is parameterized.

 2.Drawer type

         The main purpose of generics is to specify what type of object the current container should hold. Let the compiler do the checking. At this time, you need to pass the type as a parameter. Just pass in whatever type you need.

2.1 Grammar 

class 泛型类名称<类型形参列表> {
// 这里可以使用类型参数
}
class ClassName<T1, T2, ..., Tn> {
}

class 泛型类名称<类型形参列表> extends 继承类/* 这里可以使用类型参数 */ {
// 这里可以使用类型参数
}
class ClassName<T1, T2, ..., Tn> extends ParentClass<T1> {
// 可以只使用部分类型参数
}
Notice:
1. <T> after the class name represents a placeholder, indicating that the current class is a generic class
Understand: [Specification] Type parameters are generally represented by a capital letter. Commonly used names are:
        E display Element
        K Display Key
        V 表示 Value
        N 表示 Number
        T 表示 Type
        S, U, V, etc. - Second, third, fourth type
2. No type conversion is required

3. Usage of generic classes

3.1 language

泛型类<类型实参> 变量名; // 定义一个泛型类引用
new 泛型类<类型实参>(构造方法实参); // 实例化一个泛型类对象

MyArray<Integer> list = new MyArray<Integer>();

         Filling in type arguments can be omitted when the compiler can deduce the type arguments from the context.

For example:

MyArray<Integer> list = new MyArray<>(); // 可以推导出实例化需要的类型实参为 Integer
  1.   Generics parameterize data types and pass them
  2.  Use <T> to indicate that the current class is a generic class.
  3.  Advantages of generics so far: data type parameterization, automatic type checking and conversion at compile time

How are generics compiled

4.1 Rubbing machine system

        Let us understand how generics are compiled?
        Use the command: javap -c to view the bytecode file. All T are < /span> . Object

        During the compilation process, we replace all T with Object. Called:erasure mechanism.

        Java’s generic mechanism is implemented at the compilation level. The bytecode generated by the compiler does not contain generic type information during runtime.         

5. Upper and lower bounds of generics

When defining a generic class, sometimes it is necessary to impose certain constraints on the type variables passed in, which can be restricted through type boundaries.

5.1 language

class 泛型类名称<类型形参 extends 类型边界> {
...
}
public class MyArray<E extends Number> {
...
}

MyArray<Integer> l1; // 正常,因为 Integer 是 Number 的子类型
MyArray<String> l2; // 编译错误,因为 String 不是 Number 的子类型

Lower bound wildcard <? super T>

// 下界通配符 <? super T>
class 泛型类名称<类型形参 extends 类型边界> {
...
}
public class MyArray<E super Number> {
...
}


	MyArray(Float) l1;// 编译正确		
	// Object 是 Number 的父类 
	MyArray(Object) l2;// 编译错误

 6.Attention

       Note: NewArray of generic type cannot be used     
public T [] array = ( T []) new Object [ 10 ]; // 是不对的
        In layman’s terms: the returned array may store any data type, it may be String , it may be , which is considered unsafe by the compiler., when running, is directly transferred to an array of type Integer Person
public T [] array ;//正确; 

        Generics provide benefits such as improved type safety, code reuse, and cleaner code. They allow you to write more general algorithms and data structures that can handle different types without sacrificing compile-time type checking. By using generics, you can create more robust and maintainable Java code.​ 

Conclusion: The related sharing of generics ends here. I hope it will be helpful to everyone’s learning. If you have any questions or different opinions, please feel free to comment in the comment area. Message, thank you for your support

Guess you like

Origin blog.csdn.net/qq_61576108/article/details/134460558
Recommended