Arrays in compiling

Simple array

1
2
3
4
5
6
7
8
void daily(){
int array[];
int size=10;
int value=123;
array = new int[size];
array[3] = value;
value = array[5];
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int[] daily();
Code:
0: bipush 10
2: istore_2
3: sipush 123
6: istore_3
7: iload_2
8: newarray int
10: astore_1
11: aload_1
12: iconst_3
13: iload_3
14: iastore
15: aload_1
16: iconst_5
17: iaload
18: istore_3
19: aload_1
20: areturn

[7] load variable 10 to create a new array.
[10] save array’s reference to array variable.
Is aload_x show that the primary type array variable is an object ?

Object array

1
2
3
4
5
6
Integer[] objectArray(){
Integer oArray[];
int count = 10;
oArray = new Integer[count];
oArray[0] = new Integer(7);
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java.lang.Integer[] objectArray();
Code:
0: bipush 10
2: istore_2
3: iload_2
// class java/lang/Integer
4: anewarray #2
7: astore_1
8: aload_1
9: iconst_0
// class java/lang/Integer
10: new #2
13: dup
14: bipush 7
// Method java/lang/Integer."<init>":(I)V
16: invokespecial #3
20: aload_1
21: areturn

Multianewarray

1
2
3
4
5
int[][][] create3darray() {
int matrix[][][];
matrix = new int[2][3][4];
return matrix;
}

javap

1
2
3
4
5
6
7
8
9
10
int[][][] create3darray();
Code:
0: iconst_2
1: iconst_3
2: iconst_4
// class "[[[I"
3: multianewarray #4, 3
7: astore_1
8: aload_1
9: areturn

Three constant used to create the 3 dimensional array.
Store to variable matrix, push to the top of stack.