数组物化在Java中 [英] Arrays are reified in Java

查看:89
本文介绍了数组物化在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近碰到的是,数组在Java中具体化。也就是说,他们知道只有在运行时的类型的信息。但我有点糊涂了这个定义。

I recently came across that, Arrays are reified in Java. That is, they know the type information only during run time. But I am a little confused with this definition.

如果数组被说成只知道在运行时类型信息,我应该从字面上可以任意值分配给任何数组,由于打字在运行时错误将仅在运行时抛出才知道。但是,这不是在真实时间的情况下。我们得到一个编译时错误了点。

If Arrays are said to know the type information only during runtime, I should literally be able to assign any values to any arrays, since the typing is known only at run time errors will be thrown at run time only. But that is not the case in real time. We get a compile time error for that.

所以,有人可以抛出这是什么意思 - 阵列物化轻?

So can someone throw light on "what does it mean by - arrays are reified"?

推荐答案

我觉得这是什么意思的是,code给定的行会抛出异常:

What I think that means is that the given lines of code will throw an exception:

String[] arrayOfStrings = new String[10];
Object[] arrayOfObjects = arrayOfStrings; // compiles fine
arrayOfObjects[0] = new Integer(2); // throws a runtime exception (ArrayStoreException IIRC)

数组是协变:字符串[]扩展对象[]。但实际类型数组在运行时是已知的,并存储一个实例是正确的类型不是企图抛出异常。

Arrays are covariant: String[] extends Object[]. But the actual type of the array is known at runtime, and an attempt to store an instance which is not of the right type throws an exception.

这篇关于数组物化在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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