1. The basic concept of generics
Like C#, Java has the concept of generics and parameterization of types. Generics in java appear after jdk5.0, but generics in java are essentially different from generics in C#. First of all, from the perspective of collection types, ArrayList<Integer> and ArrayList<String> in java are the same type. Type erasing will be performed during compilation, and the types in java are pseudogenerics. Pseudogenerics will be introduced later. Secondly, when adding basic types of data to a collection, such as int, int will first be converted into an Integer object, which is what we usually call the boxing operation. When taking out elements, the Interger object needs to be converted into an int value type, that is, unboxing operation. In c#, List<int> and List<string> are different types. The generic parameters will be a placeholder after compilation and are not erased. They are assigned real types at runtime. They are generated during the system runtime and have their own virtual method tables and type data. This implementation is called type inflation (for type inflation, the instant compiler has done a lot of optimization work to solve this problem). This is the so-called true generics. At the same time, when adding basic elements such as int to a collection, there is no need to box, and no unboxing is required when taking out elements. Therefore, performance is better than Java collection generics.
The introduction of generics in Java is mainly to solve two problems: 1. Type replacement exceptions occur during the runtime of the collection type element, and type checks are added during the compile-time type. 2. Repeat code writing during the solution, and the algorithm can be reused. The following is an example to illustrate the type check of the compiler.
First, let's look at an example where generics are not used:
ArrayList al = new ArrayList(); al.add("abc"); al.add("124"); al.add("32L");We can add any type of data to the al collection. When we need to convert the type when fetching the data, such as:
String s = (String)al.get(0);String s1 = (String)al.get(1); //In the run period, an error will be reported and the type conversion error will be Long l = (Long)al.get(2);
From this we can see that when there are no generics, type checking is reduced during compilation. When taking out elements, programmers need to know the types of each element, otherwise an exception of type conversion may occur at runtime.
So let’s take a look at the benefits it brings to us through generic collections.
ArrayList<String> al1 = new ArrayList<String>();al1.add("abc");al1.add(1); // An error occurred during compilation,When we instantiate al1 with the String parameter type, we cannot add an int element, otherwise the compiler will report an error. Usually, there will be an error mark in the IDE editor, such as eclipse. At the same time, there is no need to type conversion when extracting the element.
string value = al1.get(0); //No type conversion is required
This is the benefit of generics.
Then the multiplexing of algorithms is mainly reflected in the multiplexing of methods, such as the Add method of ArrayList, which can be used on any type or a limited type.
2. Use of generics
Generics in java are mainly used in classes, methods, and interfaces. First, let's take a brief look at the use of the class:
class Factory<T>{ private T value; public T getValue() { return value; } public void setValue(T v) { this.value = v; } }Add test method:
Factory<String> f = new Factory<String>(); f.setValue("factory in use"); System.out.println(f.getValue());Use of generic interfaces:
interface MyInterface<T,U>{ void show(T t, U u); } class ShowTest implements MyInterface<String,Integer>{ @Override public void show(String t, Integer u) { System.out.println(t); System.out.println(u); } }When a generic type parameter acts on a class, it mainly restricts the type constraints between multiple fields and method signatures. When acting on a method, the main constraints are made on multiple parameters of the method. Here, the generic type parameters of the method will no longer be given examples. Let us mainly introduce the constraints of type parameters below.
3. Type parameter constraints
Let's look at a small example, as shown in the following code:
public static <T> T get(T t1,T t2) { if(t1.compareTo(t2)>=0);//Compilation error, the method compareTo(T) is undefined for the type T. return t1; }You can see the compiler error message. The compareTo method is not defined for type T. If the types need to be compared in Java, the Comparable interface needs to be implemented, so that the method is overridden. Then we make the following modifications:
public static <T extends Comparable> T get(T t1,T t2) { //Add type limitation if(t1.compareTo(t2)>=0); return t1; }By qualifying T extends Comparable, it shows that T is the type of interface that implements Comparable, and therefore also implements the compareTo method, so there will be no compile-time errors.
When we use & to segment multiple limitations of types, we can use & to segment the limited keywords, and only extends can be used. At the same time, when both interfaces and types exist, the class can only be placed first and can only have one, as shown below:
<T extends Object&Comparable&Serializable>
The above article briefly discusses the role and basic concepts of java generics is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.