newarray :
allocate new array for numbers or booleans : index : visitIntInsn()
- Description
- newarray is used to allocate single-dimension arrays of
booleans, chars, floats, doubles, bytes, shorts, ints or longs. For example,
when you write the Java code:
new int[10];
this
is compiled into:
bipush 10 ; push the int 10 onto the stack
newarray int ; make an array of 10 ints. The new array is left on the stack
newarray
pops a positive int, n, off the stack, and constructs an array for holding n
elements of the type given by <type>. Initially the elements in the array
are set to zero. A reference to the new array object is left on the stack. - Exceptions
- NegativeArraySizeException - n is less than zero.
OutOfMemoryError - insufficient memory to allocate the array. - Example
; This is like the Java code:
; short x[] = new short[2];
iconst_2 ; push 2 onto the stack
newarray short ; call newarray to make a 2-element short array
astore_1 ; store the reference to the array in local variable 1
- Notes
- The Java boolean false is represented in the JVM by the integer
0; true is represented by the integer 1. Boolean arrays are actually
treated as byte arrays and are initialized to false (0). You should use the
baload and bastore instructions to load and store values in
boolean arrays.
- See also
- anewarray multianewarray new
- Stack
Before
|
After |
n |
arrayref |
... |
... |
- Bytecode
Type
|
Description |
u1
|
newarray
opcode = 0xBC (188)
|
u1
|
array-type
(see below)
|
In bytecode, the type of the array is specified by the array-type byte
immediately following the newarray opcode, which has one of the
following values:
Array Type
|
Value |
boolean |
4 |
char |
5 |
float |
6 |
double |
7 |
byte |
8 |
short |
9 |
int |
10 |
long |
11 |