157.

prev next contents
newarray

allocate new array for numbers or booleans

Jasmin Syntax
     newarray <type> 
where <type> is one of: boolean, char, float, double, byte, short, int or long.

Stack

Before

After
n
arrayref
...
...
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.

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 
Exceptions

NegativeArraySizeException - n is less than zero.

OutOfMemoryError - insufficient memory to allocate the array.

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
See Also

anewarray, multianewarray, new

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.


prev next contents
Java Virtual Machine, by Jon Meyer and Troy Downing, O'Reilly Associates


Java Virtual Machine
Java Virtual Machine (Java Series)
ISBN: 1565921941
EAN: 2147483647
Year: 1996
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net