如何阵列工作的Java字节code [英] How does Arrays work in the ByteCode of Java
问题描述
如果我用一个普通的类像列表,向量或别的东西,我得到一个尺寸()
函数返回都把类的长度,但如果我使用一个类或默认的数据类型的数组,我得到它返回数组的当前长度的公共成员的长度。
If I use a normal class like List, Vector or something else, I get a size()
function which returns the length of the regarded class but if I use an array of a class or a default data type I get a public member length which returns the current length of the array.
int a[] = new int[3];
a.length; // used without ()
Vector<Integer> v = new Vector<Integer>();
v.length(); // used with ()
这是为什么?我的意思是一个数组不是自己的阶级是不是?因此,如果不用上课就不能有一个成员varaible。我不能得到一个想法,怎么说是在后台(字节code)处理。我知道,在存储器阵列中存储的指针数组的第一元素和具有索引(i)该存储器指针被移动到 ArrayPointer + I *(数据类型的大小)
。
现在你可以说电脑进入throught数组中的所有元素,并计算所有的元素,但一台电脑怎么能知道一个数组结束,下开始在哪里?并从那里来自那里的大小存储阵列的构件varaible'?
Now you can say the computer goes throught all elements of an array and counts all the elements but how can a computer know where an array ends and where the next starts? And from where comes the 'member varaible' from the array where the size is stored?
我的意思是,我们使用数组常常但我知道这么少的字节code在Java code后面会发生什么。你能向我解释如何这是可能的?
I mean we use arrays so often but I know so little what happens behind the Java code in the ByteCode. Can you explain to me how that is possible?
推荐答案
数组是在Java对象,但他们不符合真正的类。实际上,在JVM隐含动态创建数组类,但由于性能原因,他们不是实际的类。
Arrays are objects in Java, but they don't correspond to real classes. Effectively, the JVM implicitly creates array classes on the fly, but for performance reasons, they aren't actual classes.
由于它们的目的,它们可以存储在对象字段并通过周围像正常。然而,他们在字节code级处理有点不同。
Since they are objects, they can be stored in Object fields and passed around like normal. However, they are treated a bit differently at the bytecode level.
首先,阵列与 newarray
, anewarray
或 multinewarray <创建/ code>分别为单一维度灵长类动物,单一维度的对象,多维数组的说明。相比之下,普通对象与
新
指令创建。
First off, arrays are created with the newarray
, anewarray
, or multinewarray
instructions for single dimension primative, single dimension object, and multidimensional arrays respectively. By contrast, regular objects are created with the new
instruction.
获取和设置元素与完成的 * aload
和 * ASTORE
的说明。
Getting and setting elements is done with the *aload
and *astore
instructions.
同时,x.length是不是一个真正的字段即可。相反,它被编译到 arraylength
指令。这可以通过编译如下code可以看出。
Also, x.length is not a real field. Instead it gets compiled to the arraylength
instruction. This can be seen by compiling the following code.
public void test(int size){
int[] x = new int[size];
String[] y = new String[size];
System.out.println(x.length);
System.out.println(y.length);
}
结果在随后的字节code
results in the following bytecode
.method public test : (I)V
.limit stack 2
.limit locals 4
iload_1
newarray int
astore_2
iload_1
anewarray java/lang/String
astore_3
getstatic java/lang/System out Ljava/io/PrintStream;
aload_2
arraylength
invokevirtual java/io/PrintStream println (I)V
getstatic java/lang/System out Ljava/io/PrintStream;
aload_3
arraylength
invokevirtual java/io/PrintStream println (I)V
return
.end method
试图通过手动创建一个字节code会导致异常访问长度
字段,因为该领域实际上不存在。
Attempting to access the length
field by manually creating bytecode will result in an exception, because the field doesn't actually exist.
.method static public main : ([Ljava/lang/String;)V
.limit stack 1
.limit locals 1
aload_0
getfield [Ljava/lang/String; length I
return
.end method
结果
Exception in thread "main" java.lang.VerifyError: Expecting reference to class i
n class ArrayTest at constant pool index 30 in method ArrayTest.main([Ljava/lang
/String;)V
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
这篇关于如何阵列工作的Java字节code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!