Table of contents
5. Upper and lower bounds of generics
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 classUnderstand: [Specification] Type parameters are generally represented by a capital letter. Commonly used names are:E display ElementK Display KeyV 表示 ValueN 表示 NumberT 表示 TypeS, U, V, etc. - Second, third, fourth type2. 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
- Generics parameterize data types and pass them
- Use <T> to indicate that the current class is a generic class.
- Advantages of generics so far: data type parameterization, automatic type checking and conversion at compile time
4 How are generics compiled
4.1 Rubbing machine system
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
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
public T [] array = ( T []) new Object [ 10 ]; // 是不对的
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