This is the first day of my participation in Gwen Challenge

Simple Use of Java Generics (Part 1)

Sync code address github.com/xyz0z0/Andr…

A simple introduction

In the development process, we often use the List method, we can use any type of List in the process of use, this is the generics to help us. Generics are designed to support different data within the same set of logic.

It is very simple to use, as defined by <>.

public class GenericActivity extends AppCompatActivity {
	/ / 1.
    private final List<String> mNameList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generic);
        mNameList.add("Jack");
        / / 2.
        List<String> list = Arrays.asList("Jack"."Joe"."David");
        Log.d("GenericActivity"."" + mNameList.size());
        Log.d("GenericActivity".""+ list.size()); }}Copy the code

Part ① of the above code uses generic classes. Now that we have generic classes, what about generic methods? Code 2 is the call to the generic method. Let’s look at the definition of the arrays.aslist method.

    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }
Copy the code

Generic types are used in static and output parameter types by declaring methods.

Custom generic classes

Having looked at the basics, let’s try customizing a generic class and use it. We defined Fruit, Apple, ShoppingCart and ShoppingCartUtil respectively. The shopping cart class is defined as a generic class, which is then used.

        ShoppingCart<Apple> shoppingCart2 = new ShoppingCart<>();
        shoppingCart2.buy(new Apple());
        ShoppingCartUtil.showSize(shoppingCart2);// error
Copy the code

After defining it, use and add an apple as described in step 1, and you can see that there is no problem. But when we call the showSize method with a ShoppingCart argument, we find an error. Compiler prompt type mismatch.

But in our definition Apple is a subclass of Fruit, why can’t ShoppingCart be replaced with ShoppingCart?

A qualifier for a generic type

This is because the mechanism used in Java generics is generic erasing. In order to be compatible with previous versions, the generics information we define is replaced with Object in the generated bytecode. At the time of retrieval, the compiler makes sure our code is correct by adding casts.

If we really need this inheritance in use, we can use wildcards to do it. For example, this method can be modified.

    public static void showSize(ShoppingCart<? extends Fruit> list) {
        Log.d("sss"."fruit size " + list.size());
    }
Copy the code

? The extends Fruit extends Fruit means that our generic type can be Fruit or a subclass, which resolves the error reported in the previous step. This is called a generic upper bound and is commonly used during development. So with an upper bound is there a generic lower bound? The answer is yes. The definition is also handy by replacing the extends above with super to indicate that a generic type can be a Fruit or parent class.

    public static void showSizeSuper(ShoppingCart<? super Fruit> list) {
        Log.d("sss"."fruit size " + list.size());
    }
Copy the code

The generic lower bound is something I rarely use in normal development, mostly in some framework. An inverse and covariant problem is involved in the problem of upper and lower bounds. We’ll talk about that next time.

This article is a personal learning record, can not avoid mistakes or not enough in-depth understanding of technical points, but also hope for criticism and advice.