Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 5
Overflow in integer operations does not throw any exception in the JVM. The result is merely truncated to fit into the result type. For example, adding shorts 0x7fff and 1 yields 0x8000. This means that the JVM will report that 32,767 + 1 = -32,768, so long as the values being added are shorts and not ints or longs. It is up to the Java programmer to make sure that the appropriate type -- int or long -- is chosen for integer arithmetic in each situation. (If long isn't long enough, the Java programmer should invent a class that implements really long integers and the operations upon them.) Integer division by zero does throw an ArithmeticException, so the programmer should keep in mind that this exception could be thrown and catch it if necessary.
| Opcode | Operand(s) | Description |
|---|
| iadd | (none) | pops two ints, adds them, and pushes the int result |
| ladd | (none) | pops two longs, adds them, and pushes the long result |
The next table shows the exception to the rule that arithmetic opcodes take their operands from the stack. The iinc opcode performs an addition on a local variable of type int. The local variable is indicated by the first byte that follows the iinc instruction in the bytecode stream. The amount to add to the local variable is taken from the second byte following the iinc instruction. The second byte is interpreted as a byte type, an eight-bit signed two's-complement number. The local variable and byte are added, and the result is written back to the local variable. This opcode can be used to change a local variable value by any number between and including -128 through 127. This opcode makes for more efficient incrementing and decrementing of variables that are used to control execution of loops, such as for or while. No exceptions are thrown.
| Opcode | Operand(s) | Description |
|---|
| iinc | vindex, const | adds const to an int at local variable position vindex |
Integer subtraction is performed on ints and longs via the following opcodes. Each opcode causes the top two values of the appropriate type to be popped off the stack. The topmost value is subtracted from the value just beneath the topmost value. The result is pushed back onto the stack. No exceptions are thrown by these opcodes.