5.4 Switches

Switches are a kind of specialized if instruction used for comparing int values. Where if instructions can branch only two ways, a switch can go to several different locations, depending on the value of its operand. The JVM has two instructions, tableswitch and lookupswitch, depending on whether the range of possible values is densely packed or sparse. Each provides a mapping between an int value and a label. It finds the label corresponding to the int operand and transfers to there.

For example, consider the fcmpg instruction. This instruction leaves 1, 0, or 1 on the stack. The if instructions can do only a two-way jump, but with the lookupswitch instruction you can directly handle each possible result:

 ; Assume a and b are floats on top of the stack    fcmpg                       ; Compare the numbers    lookupswitch         1:       less          ; Go to less if a<b         0:       equal         ; Go to equal if a==b         1:       greater       ; Go to greater if a>b         default: fail          ; Not expecting this, but default                                ; is required less:                          ; Push "<" if a<b    ldc "<"    goto printItOut equal:                         ; Push "==" if a==b    ldc "=="    goto printItOut greater:                       ; Push ">" if a>b    ldc ">"    goto printItOut fail:                          ; Something bad has happened. Control                                ; should never get here. 

The default case is required, and it has to come last. If the value on top of the stack matches any of the keys, then control transfers to the corresponding label. If it doesn't match any of them, control transfers to the label specified by default.

The tableswitch instruction is similar to lookupswitch, except that instead of having to specify pairs of keys and labels, you have to specify only a base value and a list of labels. The first label corresponds to the base, the second to base+1, and so on. The lookupswitch code can be written instead as

 tableswitch  1    less                        ; Go to less on  1    equal                       ; Go to equal on 0    greater                     ; Go to greater on 1    default: fail               ; Fail otherwise 

The tableswitch instruction is usually faster than lookupswitch. That's because it can be implemented in the JVM by using the value as an index into a table, which is very fast. For lookupswitch, it may have to compare the value to each of the keys. However, a tableswitch instruction might take up a lot of space if the only values you really wanted to check were 22, 197, and 2,000,163.

The virtual machine's implementation of lookupswitch might not be as slow as you'd expect, however. Finding the right match is a search problem, and the virtual machine implementor might use a binary search or hash table lookup to make the search much faster than comparing individual values.

Exercise 5.4

Rewrite your answer to exercise 5.1 using a tableswitch.

Exercise 5.5

Rewrite your answer to exercise 5.1 using a lookupswitch.

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

Similar book on Amazon

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