How to instantiate generic objects in Java?

In Java, you can instantiate generic objects in the following way:

  1. brand new
ArrayList<String> list = new ArrayList<String>();
  1. Can you rephrase this in English?
  2. fresh
ArrayList<?> list = new ArrayList<>();
  1. type which is extended
  2. super type
  3. fresh
ArrayList<? extends Number> list = new ArrayList<>();
ArrayList<? super String> list = new ArrayList<>();
  1. Create a generic method: It is possible to define generics within a method and instantiate objects inside the method.
public <T> ArrayList<T> createArrayList() {
    return new ArrayList<T>();
}

These are some common ways to instantiate generic objects according to specific needs and usage scenarios.

bannerAds