“This is the 20th day of my participation in the August Gwen Challenge.
The generic
The concept of generics
Generics are an unknown data type and are used when we do not know what data type to use.
Generics can also be thought of as variables that receive data types.
We often have
- E E: Element indicates an Element
- T T: Type Indicates the Type
Note: When we create the collection object, we determine the data type of the generic.
The benefits of using generics
We know that any object can be stored in the collection. As long as the objects are stored in the collection, they will be promoted to the object type at this time. When we take out each object and carry out the corresponding operation, type conversion must be adopted.
The following examples illustrate the benefits of using generics.
Public static void main(String[] args) {public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("abc"); list.add(1); Iterator = list.iterator(); Iterator = list.iterator(); While (iterator.hasnext ()){Object next = iterator.next(); System.out.println(next); String s = (String)next; String s = (String)next; // A ClassCastException type conversion exception is thrown. System.out.println(s.length()); }}Copy the code
Here we can see that not using generics is unsafe and prone to throwing exceptions.
What good is that?
ArrayList<String> list = new ArrayList<>(); list.add("abc"); list.add(1); //errorCopy the code
Using generics allows errors at runtime to be detected at compile time.
So there’s nothing wrong with generics? B: of course. With generics, you can only store data of the same type in a collection.
The wildcard character of the generic
When using a generic class or interface, the type of the generic is not determined. In this case, the wildcard
, but once the wildcard is used, you can only use methods in the Object class, not the collection elements themselves.
The basic use of wildcards
Generic wildcards: can be used when you do not know what type to use for receiving? ,? Represents a positional wildcard
Note:
- Cannot be used when creating objects.
- Can only be used as an argument to a method.
Take a look at the following example to make it easier to understand.
public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(); list1.add("abc"); list1.add("def"); ArrayList<Integer> list2 = new ArrayList<Integer>(); list2.add(2); list2.add(3); /* We want to define a method to iterate over a collection of arbitrary types. If we don't know what type the collection is, we can use wildcards as arguments to the method. */ Print(list1); Print(list2); } private static void Print(Collection<? > list) { Iterator<? > iter = list.iterator(); while(iter.hasNext()){ Object next = iter.next(); System.out.println(next); }}Copy the code
Advanced use of wildcards
In Java generics, you can specify the upper and lower limits of a generic.
Upper limits on generics:
- Format: Type name
<? Super class >
The name of the object - Meaning: Only this type and its parent can be accepted.
The lower limit of generics:
- Format: Type name
<? Extends class >
The name of the object - Meaning: Only this type and its subclasses can be accepted.
Again, let’s do it by example
Public static void main(String[] args) {// There are four classes: Integer Number String Object // Number of the parent class of the Integer, Object ArrayList<Integer> List1 = new ArrayList<>(); ArrayList<String> list2 = new ArrayList<>(); ArrayList<Number> list3 = new ArrayList<>(); ArrayList<Object> list4 = new ArrayList<>(); getElement1(list1); //error getElement1(list2); //error getElement1(list3); getElement1(list4); getElement2(list1); getElement2(list2); //error getElement2(list3); getElement2(list4); //error } private static void getElement1(Collection<? super Number> list) { } private static void getElement2(Collection<? extends Number> list) { }Copy the code
We see some errors and some non-errors, which is the effect of receiving upper and lower limits on generics.
Write in the last
Okay, so this is an introduction to generics, the benefits and drawbacks of generics, and the use of generic wildcards. In the next article, we’ll take a closer look at the use of generics.
If the above content is not correct, welcome to dig friends to criticize.