Chapter 5. Control Instructions

A program needs to be able to repeat code and skip over sections of the code. High-level languages like Java provide constructs such as for, if, and while for structured programming. Control structures like for, if, and while make it easy to see where control of a program goes, because programs can enter each loop only at the beginning and leave at the end. The idea behind structured programming is that larger units of code are divisible into smaller units, which are themselves composed of yet smaller units.

The JVM does not support structured programming directly. The only unit of code is the instruction. In place of structured programming constructs, the JVM provides instructions to jump directly from one place in the method to another. Most of these instructions transfer control to another location if some condition is met; otherwise, the program continues at the next instruction. In Oolong, the locations are written as identifiers called labels. The process of transferring control from one instruction to another is called branching.

Labels are written as an identifier followed by a colon (:). In bytecodes, the label is transformed into an offset (either positive or negative) between the beginning of the if instruction and the beginning of the destination instruction. The offset must point to the beginning of an instruction. If it points to the middle of an instruction, the virtual machine assumes that the program is corrupted or otherwise suspicious, and will refuse to load the class.



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