What are java generics
Generics, that is, "parameterized types". When it comes to parameters, the most familiar thing is that there are formal parameters when defining a method, and then the actual parameters are passed when the method is called. So how do you understand parameterized types? As the name suggests, is to parameterize the type from the original specific type, similar to the variable parameters in the method. At this time, the type is also defined in parameter form (can be called a type parameter), and then Pass in the specific type (type argument) when using/calling.
The essence of generics is to parameterize types (the types of specific restrictions on formal parameters can be controlled through different types specified by generics without creating a new type). That is to say, during the use of generics, the data type of the operation is specified as a parameter. This parameter type can be used in classes, interfaces, and methods, and are called generic classes, generic interfaces, and generic methods respectively. (Recommended tutorial: java tutorial)
The role of generics:
The compiler checks whether the elements we put into the container meet the expectations of the generic container definition. We only need to tell the compiler what type of container this container is. The compiler automatically casts elements taken from the container.
After the intervention of generics, the programmer's focus has changed from 2 points to 1 point:
Define the type of container processing, so that the inspection of putting into the container and the transformation of taking out the container are all interchanged. It's done by the compiler.
Advantages:
1. Type safety
The main goal of generics is to improve the type safety of Java programs. By knowing the type restrictions of variables defined using generics, the compiler can verify type assumptions at a very high level. Without generics, these assumptions exist only in the minds of system developers.
Generics allow the compiler to enforce these additional type constraints by capturing this additional type information in the variable declaration. Type errors are now caught at compile time instead of being displayed as a ClassCastException at runtime. Moving type checking from runtime to compile time helps Java developers find errors earlier and more easily, and can improve program reliability.
2. Eliminate casts
A side benefit of generics is the elimination of many casts in the source code. This makes the code more readable and reduces the chance of errors. Although reducing casts can improve code that uses generic classes, there is a corresponding penalty when declaring generic variables. Using a generic variable once in a simple program does not reduce the code's popularity. But for large programs that use generic variables multiple times, the level of likes can be reduced cumulatively. Therefore, after generics eliminate forced type conversion, the code will be clearer and cleaner.
3. Higher operating efficiency
In non-generic programming, passing a single type as an Object will cause Boxing and Unboxing operations. This Both processes have significant overhead. After the introduction of generics, there is no need to perform Boxing and Unboxing operations, so the operating efficiency is relatively high. Especially in systems where collection operations are very frequent, the performance improvement brought by this feature is more obvious.
4. Potential performance gains
Generics bring the possibility of greater optimization. In the initial implementation of generics, the compiler inserts casts (without generics, Java system developers would specify these casts) into the generated bytecode. But the fact that more type information is available to the compiler opens up the possibility of optimizations in future versions of the JVM.
The above is the detailed content of what are java generics. For more information, please follow other related articles on the PHP Chinese website!