Arrays are objects in Java, but they do not correspond to real classes. In fact, the JVM implicitly creates array classes on the fly, but for performance reasons, they are not actual classes.
, Object , . , - -.
-, newarray, anewarray multinewarray , . , new.
*aload *astore.
, x.length . arraylength. , .
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);
}
-
.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
length - , .
.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)