3.6 Constants

Constants are particular numbers and strings referred to by your program. The JVM supports constants of type int, float, long, double, and String. This section shows how to load constants onto the stack in Oolong.

3.6.1 General Constants

The ldc and ldc2_w instructions (for "load constant") are used to push a constant value onto the stack. These constants are specified in Oolong the same way they are in Java. The ldc instruction is used for int, float, and String constants, and ldc2_w is used with long and double constants.

Here are some examples of loading constants:

 ldc "Hello, world"        ; Push a String onto the stack ldc 1.0                   ; Push a float onto the stack ldc2_w 2.7182818284D      ; Push a double onto the stack ldc2_w 1234567890L        ; Push a long onto the stack ldc 2                     ; Push an int onto the stack 

The values loaded with ldc2_w each take up two entries on the stack; that's what the 2 stands for. The others take up one. If you were to execute all the instructions in this example one after the other, they would take up seven stack slots.

The _w part of ldc2_w stands for "wide." When the Oolong assembler builds the class file, it uses two bytes to form a reference to a long or double value in the constant pool. The constant pool contains the actual long or double value that is loaded onto the stack. The ldc2_w instruction takes up more space in the class file than the ldc instruction, which uses only one byte to form its constant pool reference. The constant pool stores all of the constants used with ldc, as well as other useful things for the class. This is explained in chapter 9.

There is also a ldc_w instruction, which is identical to ldc except that it also uses two bytes instead of one for specifying the index into the constant pool. Oolong converts between ldc and ldc_w as necessary, depending on where the constant value ends up in the constant pool.

3.6.2 Small Constants and null

Some numbers come up a lot. Small numbers are more likely to be mentioned than large numbers; 0 and 1 come up a lot, but 7,349,934 probably isn't mentioned explicitly in any program anywhere.

The JVM is optimized for working with small constants by providing instructions specifically for loading them. This eliminates the need for adding extra entries for the constant pool. Because of this, ldc and ldc2_w are actually fairly uncommon instructions, though you could use them anywhere you would use the specialized instructions.

For the most common numbers there are the xconst_n instructions, where x represents the type, and n is the value to push. For example,

 fconst_1         ; Equivalent to ldc 1.0 iconst_m1        ; Equivalent to ldc  1 lconst_0         ; Equivalent to ldc 0L 

The full set of constant-pushing instructions is found in Table 3.3.

For small integers outside this range you can use bipush (for values between 128 and 127) and sipush (for values between 32,768 and 32,767). These take the values as an argument, instead of requiring a constant pool entry. In bytecodes, bipush takes a one-byte argument, and sipush takes a two-byte argument. These instructions all have the effect of pushing the int 5 onto the stack:

Table 3.3. Constant-loading instructions
Mnemonic Argument Effect
aconst_null   Push a reference to null onto the stack.
iconst_m1   Push an int 1 onto the stack.
iconst_0   Push an int 0 onto the stack.
iconst_1   Push an int 1 onto the stack.
iconst_2   Push an int 2 onto the stack.
iconst_3   Push an int 3 onto the stack.
iconst_4   Push an int 4 onto the stack.
iconst_5   Push an int 5 onto the stack.
lconst_0   Push a long 0 onto the stack.
lconst_1   Push a long 1 onto the stack.
fconst_0   Push a float 0 onto the stack.
fconst_1   Push a float 1 onto the stack.
fconst_2   Push a float 2 onto the stack.
dconst_0   Push a double 0 onto the stack.
dconst_1   Push a double 1 onto the stack.
bipush n Push n onto the stack, 128 n 127.
sipush n Push n onto the stack, 32,768 n 32,767.

 iconst_5         ; Requires 1 byte ldc 5            ; Requires 2 bytes and a constant pool entry bipush 5         ; Requires 2 bytes sipush 5         ; Requires 3 bytes 

In addition to the commonly used numbers, there is one reference value that is used frequently: null. Use the aconst_null instruction to push a null onto the stack.

Exercise 3.1

Replace each of the following instructions with the shortest equivalent:

 sipush 9 bipush 4 ldc 1.0 ldc -901 ldc 123456 

Exercise 3.2

How many stack slots are consumed by the instructions in exercise 3.1?



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

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