The concept and usage of generics in Java language


highlight: a11y-dark

theme: channing-cyan

Keep creating and accelerate growth! This is my 20th day participating in the "Nuggets Daily New Plan·October Update Challenge", click to view event details

Analyze code

Before understanding what generics are, I want you to analyze the following code first. Think about whether the code will report an error and what error will be reported. ```java import java.util.ArrayList;

public class ListTest{ public static void main(String[] args) { //The array queue saves data ArrayList arrayList=new ArrayList (); //name arrayList.add("Zhang San"); //age arrayList.add ( 18 ); //sex arrayList.add ("male"); String msg; for (int i = 0; i < arrayList.size (); i++) { msg=(String)arrayList.get ( i ); System.out .println (msg); } } } ``` The answer seems obvious. When we print, the following error message will appear:

image.pngIn the above coding and error reporting, we can find the following two problems: 1. When objects of various data types are put into the array, the array will not remember the type of the object. When the object is taken out again, the object It becomes an object type , but when it is run, the object's type is still the data type when it was added to the array. 2. When we take out the array elements, we need to manually cast them to a specific target type. However, because there is numerical (integer) data, we simply msg=(String)arrayList.get ( i );cast all the data and an error will be reported.

Generics

Generics proposed by the Java language are used to avoid the above errors. Generics mean parameterized types , and parameterized types mean that the type is parameterized from the original specific type, similar to the variable parameters in the method. At this time, the type is also defined in parameter form , and then the specific type is passed in when called. Parameter type .

Generic definition

In the Java language, generics are defined as follows: java public class 泛型类类名<T>{ //.... } the method of declaring a generic variable and assigning a value: java 泛型类类名<具体类名> 变量名 = new 泛型类类名<>(参数列表); - Note: <T> declaration is a mark of generics, T can be the type of a member attribute, the return type of its internal method, the type of method parameters . The parameter list of the constructor of a generic class must contain constants or variables whose data types are other class names.

  • Generic interface definition

```java public interface interface name { public void method 1 (parameter list); public T method 2 (parameter list); //...... }

  • Generic method definition java public void method name (parameter list) { //... } ```
  • Call a generic method

java 方法名(实参参数列表); - Note: <T> is just a marker for a generic method , and T is the return type . Generic methods are only visible in generic classes or generic interfaces . T can appear in the parameter list of a generic method.

Guess you like

Origin blog.csdn.net/y943711797/article/details/132972180