Java中ArrayList的实现中的类型擦除 [英] type erasure in implementation of ArrayList in Java

查看:110
本文介绍了Java中ArrayList的实现中的类型擦除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java泛型上阅读了这篇文章提到ArrayList的构造函数看起来像这样:

I was reading this article on Java Generics and there it is mentioned that the constructor for an ArrayList looks somewhat like this:

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

我无法理解编译器如何进行类型擦除和类型检查,如此处所述.我得到的一点是,将类型参数转换为Object类型.

I was not able to understand how type erasure and type checking by the compiler happens as explained there. One point I got was that the type parameter is transformed to Object type.

我会想象为(用Object替换所有V),但这绝对是错误的.

I would imagine it as (replacing all V with Object), but this definitely wrong.

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

如何精确转换为Object类型,但仍保留V的类型安全性? 当我有ArrayList<String>ArrayList<Integer>时,每个都有两个不同的类吗?如果没有,StringInteger的类型信息存储在哪里?

How exactly is it transformed to Object type but still retaining the type safety for V? when I have ArrayList<String> and ArrayList<Integer> are there two different classes for each? If not, where is the type information of String and Integer is stored?

推荐答案

您的类型擦除版本不正确.类型参数声明不会删除为Object,而只会删除其用法.更具体地说:

Your type erased version is not correct. The type parameter declaration is not erased to Object but only it's usage is erased. More specifically:

  • 对普通类型的擦除是其对应的原始类型.因此,对于ArrayList<V>,它就是ArrayList.
  • 删除类型参数是它的最左边界.
  • 所有类型参数都将被删除.类型参数是您在实例化泛型类时使用的参数.因此,ArrayList<Integer>将替换为ArrayList.
  • Erasure of a generic type is its corresponding raw type. So, for ArrayList<V>, it would be just ArrayList.
  • Erasure of a type parameter is its left-most bound.
  • And all the type arguments are just removed. The type arguments are the one you use while instantiating the generic class. So, ArrayList<Integer> will be replaced with ArrayList.

因此,正确的已删除版本将是:

So, the correct erased version would be:

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

当我有ArrayList和ArrayList时,每个都有两个不同的类吗?

when I have ArrayList and ArrayList are there two different classes for each?

不,从来没有这样.编译器仅生成泛型类型或方法的一个字节代码表示形式,并将泛型类型或方法的所有实例化映射为唯一表示形式.

No, this is never the case. The compiler generates only one byte code representation of a generic type or method and maps all the instantiations of the generic type or method to the unique representation.

是否不存储String和Integer的类型信息?

if not where the type information of String and Integer is stored?

当编译器执行类型擦除时,它会根据一些预定义的规则删除所有类型信息,偶尔会添加所谓的 bridge方法,并添加所需的所有必需的类型转换.

When the compiler performs type-erasure, it removes all the type information, based on some pre-defined rules, occasionally adding what is called as bridge method, and adds all the necessary type casting required.

例如,ArrayList<Integer>ArrayList<String>的以下用法:

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);

将被转换为如下形式:

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);

进一步阅读:

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

这篇关于Java中ArrayList的实现中的类型擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆