anewarray : allocate new array for objects : index : visitMultiANewArrayInsn()

Description
anewarray allocates a new array for holding object references. It pops an int, size, off the stack and constructs a new array capable of holding size object references of the type indicated by <type>.

<type> indicates what types of object references are to be stored in the array (see aastore). It is the name of a class or an interface, or an array type descriptor. If it is java/lang/Object, for example, then any type of object reference can be stored in the array. <type> is resolved at runtime to a Java class, interface or array. See Chapter 7 for a discussion of how classes are resolved.

A reference to the new array is pushed onto the stack. Entries in the new array are initially set to null.
Exceptions
NegativeArraySizeException - size is less than zero
Example
; Allocate a 10-element array of for holding references to
; Threads. This is like the Java code:
;      Thread x[] = new Thread[10];
bipush 10
anewarray java/lang/Thread
astore_1    ; store the new array in local variable 1
; Allocate a multi-dimensional array like:
;       new String[2][5]
; using anewarray. First, allocate new 2-element array for holding 
; arrays of strings and store it in local variable 1.
iconst_2
anewarray [Ljava/lang/String;      ; type descriptor for array-of-String
astore_1
; next, allocate first array of String[5] and store it in index 0
aload_1
iconst_0
bipush 5
anewarray java/lang/String
aastore
; finally, allocate second array of String[5] and store it in index 1
aload_1
iconst_1
bipush 5
anewarray java/lang/String
aastore
Notes
It is more efficient to use multianewarray to allocate multi-dimensional arrays.
See also
newarray multianewarray new
Stack
Before After
size arrayref
... ...
Bytecode
Type Description
u1 anewarray opcode = 0xBD (189)
u2 index