Implementing type erasure of ArrayList in Java

I read this article on Java generics and mentioned that the constructor of ArrayList looks a bit like this:

class ArrayList<V> {
  private V[] backingArray;
  public ArrayList() {
    backingArray = (V[]) new Object[DEFAULT_SIZE]; 
  }
}

I can't understand how to interpret compiler type erasure and type checking What I get is that the type parameter is converted to the object type

I'd imagine it (replacing all V's with object), but it's definitely wrong

class ArrayList<Object> {
      private Object[] backingArray;
      public ArrayList() {
        backingArray = (Object[]) new Object[DEFAULT_SIZE]; 
      }
}

How does it convert to an object type but still keep V's type safe? When I have ArrayList < string > and ArrayList < integer >, do each have two different classes? If not, where is the type information for storing string and integer?

resolvent

Solution

Your type has been erased. The version is incorrect The type parameter declaration is not cleared to the object, only its use is deleted Further:

>The erasure of a generic type is its corresponding original type Therefore, for ArrayList V, it will only be ArrayList. > The erasure of a type parameter is its leftmost boundary. > All type parameters are deleted Type parameters are parameters used when instantiating generic classes Therefore, ArrayList < integer > will be replaced by ArrayList

Therefore, the correct erase version will be:

class ArrayList {
    private Object[] backingArray;
    public ArrayList() {
      backingArray = (Object[]) new Object[DEFAULT_SIZE]; 
    }
}

No, it will never happen The compiler generates only one bytecode representation of a generic type or method and maps all instances of the generic type or method to a unique representation

When the compiler performs type erasure, it will delete all types of information according to some predefined rules, occasionally add the so-called bridge method, and add all required type conversions

Therefore, for example, the following usage of ArrayList < integer > and ArrayList < string & gt;::

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
int value = list.get(0);

ArrayList<String> list2 = new ArrayList<String>();
list.add("A");
String value2 = list.get(0);

Will be converted to something like this:

ArrayList list = new ArrayList();
list.add(1);
int value = (Integer) list.get(0);

ArrayList list2 = new ArrayList();
list.add("A");
String value2 = (String) list.get(0);

Further reading:

> Java Generics FAQs – What is Type Erasure? > Why does compiler adds cast while translating generics?

The above is all the contents of the type erasure of ArrayList in Java collected and sorted out by the programming home for you. I hope this article can help you solve the program development problems encountered in realizing the type erasure of ArrayList in Java.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>