繁体   English   中英

Java将Integer转换为String不一致

[英]Java conversion Integer to String inconsistency

为什么从整数的集合转换为String的集合有效,但是当我实际上必须将Integer转换为String时却失败了? 为什么要使它失败/更早捕获? 在下面的示例中

static <T extends List<?> >
    void testConversion(T... args)
    {
        **// Didnt catch here?**
        List<String>[] slarray = (List<String>[])args;
        System.out.printf("Value is %s\n", slarray[0].get(0));
        **// Didnt catch here?**
        List<String> slist = (List<String>)args[0];
        // FAIL runtime exception
        String s2 = slist.get(0);
        // FAIL exception
        String s = slarray[0].get(0); 
    }

public static void main( String[] args )
    {
        testConversion(Arrays.asList(11,12), Arrays.asList(21,22));
    }

这是因为Java泛型是编译时的东西,程序运行时会删除实际类型。

您将T定义为“任何清单”。 然后,将对“任何列表”的引用转换为“字符串列表”。 这种类型转换可能会起作用(因此,这不是编译时错误),因此编译器不会抱怨,而是将问题留给了运行时系统。

在运行时,它只是对List的引用。 因此,运行时环境也不会抱怨-您投射了一个List数组并将其分配给List数组,此时实际类型已被擦除。

但是当涉及到获取实际值时,运行时环境可以看到该值实际上无法分配给String,因此它引发了异常。

当您在代码中提供强制转换时,您会告诉编译器:“我知道您有x的实例,但我知道它确实是y 。请将其视为y 。” 如果完全可以进行强制转换,则编译器将允许它。

您正在告诉编译器可以将args (它知道是T[]强制转换为List<String>[] 您还告诉编译器args[0] (它接受为T )是List<String>

发生这种情况是,在调用testConversion ,您正在传递List<Integer> 类型T推断为Integer 但是,由于类型擦除,在运行时它们实际上是List 另外,对List<String>[]List<String>成功,因为在运行时,它们实际上分别对List[]List转换。

编译时,应该收到未经检查的有关转换为List<String>[]List<String>强制转换警告。 它警告您有关类型安全。 它还在get的调用上插入了对String的强制转换,因为您告诉它它是List<String> 当您最终尝试从List<String>获取String时,JVM抛出了运行时异常,因为该元素确实是Integer

args转换为List<String>[] ,以及将args[0]强制转换为List<String> ,您都对编译器撒谎,但是JVM欺骗了您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM