1. Generics are a new feature of Java SE 1.5. The nature of generics is parameterized typing, which means that the data type being operated on is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively. The benefit of the introduction of generics into the Java language is security and simplicity.

Prior to Java SE 1.5, parameters were “arbitrary” by referring to the type Object in the absence of generics. The disadvantage of “arbitrary” was to do explicit casts that required the developer to be able to predict the actual parameter types. In the case of a cast error, the compiler may not give an error until runtime, which is a security risk.

The benefit of generics is that type safety is checked at compile time, and all casts are automatic and implicit to increase code reuse.

The above content is from Baidu Baike

Here’s an example:

The Box class is defined as a generic class

public class Box { private T object; public void set(T object) { this.object = object; } public T get() { return object; }} Create a Box object with no generic parameters

Box box2 = new Box(); box2.set(new Apple()); Apple apple = (Apple) box2.get(); Create a Box object with generic parameters so that no casting is required when fetching the object

Box box = new Box(); box.set(new Apple()); Apple apple = box.get(); To summarize the benefits of generics

Eliminates casts, allows type safety checks at compile time, and can be used on classes, methods, and interfaces

But when we’re defining generic classes, generic methods, generic interfaces we’re going to see a lot of different wildcards T, E, K, V, and so on, and what do they all mean? Keep reading

  1. I’m going to talk about the difference between the generic wildcards T, E, K, and V. All of these are Java generic wildcards. At first, I saw so many wildcards that I was confused

Use capital letters A,B,C,D…… X,Y, and Z are all generics, and the same thing happens when you replace T with A, where T is just A name

? T (type) represents a specific Java type K (key value) V (Key value) represents the key value in Java and E (Element) represents element. For example:

public class Test { public List list = new ArrayList(); public static void main(String[] args) { Test test = new Test(); test.list.add(“hello”); System.out.println(test.list); }} and

public class Test { public List list = new ArrayList(); public static void main(String[] args) { Test test = new Test(); test.list.add(“hello”); System.out.println(test.list); }} if T is replaced by A, there is no difference in execution effect, but we have agreed that T represents type, so it is better to follow the convention specification, which increases the readability of the code.

If you want to define multiple generic parameters, say, two generic parameters

A typical example is Map key and value generics, and we can define one as well

public interface Mymap<K, V> { public K getKey(); public V getValue(); } public class MymapImpl<K, V> implements Mymap<K, V> { private K key; private V value; public MymapImpl(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; }} then you can pass in any type and create an instance without converting the type

Mymap<String, Integer> mp1= new MymapImpl<String, Integer>(“Even”, 8); Mymap<String, String> mp2= new MymapImpl<String, String>(“hello”, “world”); Mymap<Integer, Integer> mp3= new MymapImpl<Integer, Integer>(888, 888); If you want to define more than two, three, or more generic parameters, use T1, T2… Tn looks something like that

public class Test<T1,T2,T3> { public void print(T1 t1,T2 t2,T3 t3){ System.out.println(t1.getClass()); System.out.println(t2.getClass()); System.out.println(t3.getClass()); List, List, List<? > ArrayList al=new ArrayList(); Specifies that collection elements must be of type T only

ArrayList al=new ArrayList(); Collection elements can be of any type. This makes no sense, usually in methods, just to illustrate usage

ArrayList<? extends E> al=new ArrayList<? extends E>();

Qualification of generics:

? Extends E: Receives type E or a subtype of E.

? Super E: Receives type E or the parent type of E

Public void printList(List List){} Public void printList(List List){} Public void printList(List List){} Public void printList(List List){} What’s the difference from T? Is an indeterminate class,? And T are both indeterminate types, but if it is T, the function can operate on T, such as T car = getCar() instead of? The car = getCar (). Here’s a comparison of the three:

package com.lyang.demo.fanxing; import java.util.Arrays; import java.util.List; / * *

  • Test the difference between generic parameters Object and T
  • Created by yanglu on 2017/04/20. / public class TestDifferenceBetweenObjectAndT { public static void printList1(List list) { for (Object elem : list) System.out.println(elem + ” “); System.out.println(); } public static void printList2(List list) { for (T elem : list) System.out.println(elem + ” “); System.out.println(); } public static void printList3(List<? > list) { for (int i = 0; i<list.size(); i++) System.out.println(list.get(i) + ” “); System.out.println(); } public static void main(String[] args) { List test1 = Arrays.asList(1, 2, 3); List test2 = Arrays.asList(“one”, “two”, “three”); List test3 = array. asList(1, “two”, 1.23); List test4 = Arrays.asList(new Apple(), new Banana()); /
  • The following statement will compile an error because the parameter cannot be converted successfully
  • */ printList1(test4); /**/ printList1(test3); printList1(test3); printList2(test1); printList2(test2); printList2(test3); printList3(test1); printList3(test2); printList3(test3); }} JAVA generic wildcard T, E, K, V
    1. And finally, T, Class, Class
      T is a concrete class, such as String,List,Map…… And so on, these are all concrete classes, this is easier to understand

    ** what is Class, Class is also a Class, but Class is the above String,List,Map…… Class information a class **, a bit abstract, let’s look at it step by step.

    There are three ways to get a Class:

    1. Call the getClass() method of the Object Class to get a Class Object, which is the most common way to generate a Class Object.

    Such as:

    List list = null; Class clazz = list.getClass(); **2. Use the static forName() method of the Class Class to get the Class object corresponding to the string.

    **

    Such as:

    Class clazz = Class.forName(“com.lyang.demo.fanxing.People”); 3. The third way to get an object of type Class is very simple. If T is a Java type, then T.class represents the matching class object.

    Class clazz = List.class; ** So the question? The Class Class is created, but the Class and Class<? > < p style = “max-width: 100%; clear: both; **

    Use Class and Class<? > < p style = “max-width: 100%; clear: both; min-height: 1em;

    People people = (People) Class.forName(“com.lyang.demo.fanxing.People”).newInstance(); You see, it has to be strong, and if the reflection type is not People, it will report

    Java. Lang. ClassCastException error.

    With Class generics, there is no need for strong casts

    public class Test { public static T createInstance(Class clazz) throws IllegalAccessException, InstantiationException { return clazz.newInstance(); } public static void main(String[] args) throws IllegalAccessException, InstantiationException { Fruit fruit= createInstance(Fruit .class); People people= createInstance(People.class); }} that Class and Class<? > < p style = “max-width: 100%; clear: both; min-height: 1em;

    When Class is instantiated, T is replaced by a concrete Class

    Class<? > < span style = “box-sizing: border-box! Important; Can represent any type and is used primarily for declarative restrictions

    For example, you can declare one

    public Class<? > clazz; But you can’t declare one

    public Class clazz; Because T needs to specify the type

    So, when you don’t know what type of Class to declare, you can define a Class. Class can be used for parameter type definition, method return value definition, etc.

    Above all the sample code I have to submit to the making, interested students can go to https://github.com/qwertyanglu/FanxingDemo

    If there is something wrong with my understanding, I would like to thank you for your valuable guidance.

    Write at the end:

    Code word is not easy to see the last, then a point of concern bai, only collection of attention is playing rogue!

    Follow and send me a private message “architecture”, free Java architecture information, first come, first served!