Android Foundation Series (1) Java generics

Time:2021-6-14

sketch

Java generics is a new feature introduced in j2se1.5. Its essence is parameterized type. That is to say, the operated data type is specified as a type parameter. This parameter type can be used in the creation of classes, interfaces and methods, which are called generic class, generic interface and generic method respectively.
Let’s understand java generics through these questions.

  1. What is the role of generics?
  2. Why are there generics?
  3. What is generic erasure?
  4. What are wildcards?
  5. What is the upper boundary? What is the lower boundary?

The role of 1 generics

First, what is the role of generics?
The simple generalization is to make the type pass like the parameter of the method.

Why are there generics?

So why there are generics? We need to see what problems generics solve.
For example, now we have a water tank, and then we put a red carp in it.

//Red carp
public class GreenCarp {
    public void move() {
        System. Out. Println;
    }
}

//Tank object
public class Tank {
    private RedCarp redCarp;

    public RedCarp getRedCarp() {
        return redCarp;
    }

    public void setRedCarp(RedCarp redCarp) {
        this.redCarp = redCarp;
    }
}

//Release fish
public class Test {
    public static void main(String[] args) {
        Tank tank = new Tank();
        tank.setRedCarp(new RedCarp());
        tank.getRedCarp().move();
    }
}

Then we want to replace the water tank with green carp. At this time, someone may directly define an object in the water tank.

public class Tank {
    private Object obj;

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }
}

public class Test {
    public static void main(String[] args) {
        Tank tank = new Tank();
        tank.setObj(new RedCarp());
        ((RedCarp) tank.getObj()).move();
        tank.setObj(new GreenCarp());
        ((GreenCarp) tank.getObj()).move();
    }
}

Android Foundation Series (1) Java generics

It seems that there is no problem, but if you use too much object, it will become a very troublesome thing for programmers. For example, it may not convert to the correct type, but the compiler will not be popular! Error after running!

public class Test {
    public static void main(String[] args) {
        Tank tank = new Tank();
        tank.setObj(new RedCarp());
        ((RedCarp) tank.getObj()).move();
        tank.setObj(new GreenCarp());
        ((RedCarp) tank.getObj()).move(); //  It's redcarp
    }
}

Android Foundation Series (1) Java generics

So, if we use generics, everything becomes different.

public class Tank<T> {
    private T t;

    public T getObj() {
        return t;
    }

    public void setObj(T t) {
        this.t = t;
    }

}

Android Foundation Series (1) Java generics

3 generic erasure

Before we talk about generic erasure, let’s look at an example,

public class Test {
    public static void main(String[] args) {
        List<String> l1 = new ArrayList<String>();
        List<Integer> l2 = new ArrayList<Integer>();
        System.out.println(l1.getClass() == l2.getClass());
    }
}

What do you think the above code will output? true or false?
Android Foundation Series (1) Java generics
The answer is true. Why? Why are the two runtime classes of L1 and L2 equal.
This is the result of generic erasure.
Although the two objects look very different at encoding time, they have been reflected to erase generics at runtime.
Example:

List<String> l1 = new ArrayList<String>();
l1.add("8848");
Class c = l1.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(l1, 8848);
System.out.println(l1);

Summary:
Generic erasure is to be compatible with the old version of JDK. While generics only exist at compile time. Generics enable the compiler to check types during compile time to improve type safety and reduce the number of exceptions caused by object type mismatch.

4 wildcards

Wildcard?

Java inheritance relations are not recognized in generics without any declaration modification. So we need generic processing.

5 upper and lower boundaries

Upper boundary

? extends TRepresents the subclass type that can be passed in t or t.

Lower boundary

? super TRepresents a parent type that must be t or t.

When will the upper boundary be used?

The upper boundary can’t be placed, because under the action of the upper boundary, we don’t know what type to put, so we will report an error, but we can take it.
Android Foundation Series (1) Java generics

When to use the border?

The lower boundary can’t be taken, because under the action of the lower boundary, we don’t know what kind of data to take out, but we can put it.
Android Foundation Series (1) Java generics

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]