You are previewing Programming for the Java™ Virtual Machine.
O'Reilly logo
Programming for the Java™ Virtual Machine

Book Description

The core of Java technology, the Java virtual machine is an abstract computing machine that enables the Java platform to host applications on any computer or operating system without rewriting or recompiling. Anyone interested in designing a language or writing a compiler for the Java virtual machine must have an in-depth understanding of its binary class format and instruction set. If you are programming with the Java programming language, knowledge of the Java virtual machine will give you valuable insight into the Java platform's security capabilities and cross-platform portability. It will increase your understanding of the Java programming language, enabling you to improve the security and performance of your programs.

The author employs a tutorial approach that provides a detailed look into the central workings of the technology and teaches the reader how to write real programs for the Java virtual machine. He describes methods for becoming a better programmer through an advanced understanding of the Java virtual machine and Java technology. Programming for the Java Virtual Machine offers comprehensive coverage of all the major elements of the Java virtual machine--classes and objects, control instructions, debugging, class loaders, compiling the Java programming language, performance issues, security, and threads and synchronization. The book provides an introduction to the Java Virtual Machine Specification (JVMS), with a collection of topics that help programmers understand the Java virtual machine and the JVMS better. In addition, the book features implementations of Prolog and Scheme, a language that runs on top of the Java virtual machine, generating Java virtual machine code as it runs and using a Java virtual machine class loader to load the generated code into the system.

You will find detailed information on such topics as:

  • The Java virtual machine verification algorithm

  • How Java virtual machine security works, and what it can and can't do

  • Using class loaders to incorporate code and dynamically generated code from the Internet, the Java Foundation Classes, database queries, and other languages

  • The mechanics of compiling the Java programming language for the Java virtual machine

  • Implementing other languages using the Java virtual machine, including Scheme, Prolog, Sather, Eiffel, and regular expressions

  • Numerous examples illustrate techniques and concepts, and exercises with solutions help you gain practical experience.



    0201309726B04062001

    Table of Contents

    1. Copyright
      1. Dedication
    2. Preface
      1. Prerequisites
      2. Books
      3. Acknowledgments
    3. 1. Introduction to the Java Virtual Machine
      1. 1.1. Java Virtual Machine and Java
      2. 1.2. What Is the Java Virtual Machine?
        1. 1.2.1. Instruction Set
        2. 1.2.2. class File Format
        3. 1.2.3. Verification
      3. 1.3. Java Platform
      4. 1.4. Organization of the Java Virtual Machine
        1. 1.4.1. Class Area
        2. 1.4.2. Java Stack
        3. 1.4.3. Heap
        4. 1.4.4. Native Method Stacks
        5. 1.4.5. Example
      5. 1.5. Garbage Collection
        1. 1.5.1. Finalization
        2. 1.5.2. Timing of Garbage Collection
      6. 1.6. JVM Implementations
      7. 1.7. Versions of the Virtual Machine
      8. 1.8. Future of the Java Virtual Machine
    4. 2. Oolong
      1. 2.1. Hello, World
        1. 2.1.1. .class and .super Directives
        2. 2.1.2. main Method
        3. 2.1.3. Instructions
      2. 2.2. Structure of an Oolong Program
      3. 2.3. Defining the Class
      4. 2.4. Superclasses
      5. 2.5. Declaring Fields
      6. 2.6. Declaring Methods
      7. 2.7. Interfaces
    5. 3. Writing Methods
      1. 3.1. Some Definitions
      2. 3.2. Types
        1. 3.2.1. Numeric Types
        2. 3.2.2. Object Types
        3. 3.2.3. Type returnAddress
      3. 3.3. Mnemonic Naming Conventions
      4. 3.4. Testing Code Examples
      5. 3.5. Returns
      6. 3.6. Constants
        1. 3.6.1. General Constants
        2. 3.6.2. Small Constants and null
      7. 3.7. Local Variables
        1. 3.7.1. Initializing Variables
        2. 3.7.2. Local Variables as Method Arguments
      8. 3.8. Math
        1. 3.8.1. Arithmetic Operations
        2. 3.8.2. Nonnumbers and Infinity
        3. 3.8.3. Bitwise Operations
        4. 3.8.4. Floating-Point Arithmetic and strictfp
      9. 3.9. Type Conversions
        1. 3.9.1. Conversion Example
        2. 3.9.2. Widening and Narrowing
      10. 3.10. Stack Manipulations
      11. 3.11. Arrays of Numbers
      12. 3.12. Just Biding Your Time
    6. 4. Classes and Objects
      1. 4.1. What Is an Object?
        1. 4.1.1. Objects and References
        2. 4.1.2. Class Names
        3. 4.1.3. Packages
      2. 4.2. Creating Objects
      3. 4.3. Constructors
      4. 4.4. Using Fields
        1. 4.4.1. Types and getfield
        2. 4.4.2. Inheriting Fields
        3. 4.4.3. Changing Field Values
      5. 4.5. Invoking Methods on Objects
        1. 4.5.1. Virtual Invocation
        2. 4.5.2. Method Inheritance and Overriding
        3. 4.5.3. Overloading and Method Matching
        4. 4.5.4. Invoking a Method Directly
        5. 4.5.5. invokespecial and super
      6. 4.6. Casting
      7. 4.7. Casting, Fields, Methods, and Java
      8. 4.8. Returning Objects
      9. 4.9. Interfaces
      10. 4.10. Static Fields and Methods
      11. 4.11. Class Initialization
      12. 4.12. Arrays
        1. 4.12.1. Arrays of Reference
        2. 4.12.2. Multidimensional Arrays
        3. 4.12.3. Length of Arrays
    7. 5. Control Instructions
      1. 5.1. Comparing Integers
      2. 5.2. Comparing References
      3. 5.3. Other Comparisons
      4. 5.4. Switches
      5. 5.5. Subroutines
      6. 5.6. Exceptions
        1. 5.6.1. Multiple Exception Handlers
        2. 5.6.2. .throws Directive
        3. 5.6.3. Other Ways to Throw Exceptions
      7. 5.7. A Gentler Way of Checking Type
    8. 6. Verification Process
      1. 6.1. How the Verification Algorithm Works
      2. 6.2. Is It a Structurally Valid class File?
      3. 6.3. Are All Constant References Correct?
      4. 6.4. Are All the Instructions Valid?
      5. 6.5. Will Each Instruction Always Find a Correctly Formed Stack and Local Variable Array?
        1. 6.5.1. Example 1: Hello, World
        2. 6.5.2. Example 2: gotos and ifs
        3. 6.5.3. Example 3: Code That Doesn't Verify
        4. 6.5.4. Example 4: Dealing with Subclasses
        5. 6.5.5. Algorithm
      6. 6.6. Do External References Check Out?
      7. 6.7. Java Language and Verification Algorithm
        1. 6.7.1. Fooling the Virtual Machine with a Java Compiler
        2. 6.7.2. Other Languages and the Verification Algorithm
      8. 6.8. Other Safety Requirements
      9. 6.9. Checking Verification
    9. 7. Debugging
      1. 7.1. Debugging Directives in Oolong
        1. 7.1.1. .source Directive and .line Directives
        2. 7.1.2. .var Directive
      2. 7.2. Debugging Oolong Programs
      3. 7.3. Runtime Tracing
    10. 8. Class Loaders
      1. 8.1. How Classes Are Loaded
        1. 8.1.1. Loading
        2. 8.1.2. Linking
      2. 8.2. Loading Classes without a Class Loader
      3. 8.3. Making Your Own Class Loader
        1. 8.3.1. Caching Classes
        2. 8.3.2. Examples
      4. 8.4. Working with Class Objects
      5. 8.5. Reflection
    11. 9. Inside a Java class File
      1. 9.1. class File Header
      2. 9.2. Constant Pool
        1. 9.2.1. UTF8 Entries
        2. 9.2.2. Constant Entries
        3. 9.2.3. Field, Method, and Class Entries
      3. 9.3. Class Information
      4. 9.4. Fields and Methods
      5. 9.5. Class Attributes
      6. 9.6. Other Attributes
        1. 9.6.1. ConstantValue Attribute
        2. 9.6.2. Exceptions Attribute
        3. 9.6.3. InnerClasses Attribute
    12. 10. Compiling Java
      1. 10.1. Expressions and Statements
      2. 10.2. Expression Types
        1. 10.2.1. Numeric Types
        2. 10.2.2. Object Types
        3. 10.2.3. Expression Result Types
        4. 10.2.4. Compile-Time Types and Type Restrictions
        5. 10.2.5. Type Safety and Verifiability
      3. 10.3. Compiling Statements and Expressions
      4. 10.4. Local Variable Declarations
      5. 10.5. Fields and Variables
        1. 10.5.1. Abbreviations
        2. 10.5.2. Other Assignments
        3. 10.5.3. Assignments as Expressions
        4. 10.5.4. Type-Checking in Assignments
      6. 10.6. Creating New Objects
      7. 10.7. Class Name Abbreviations
      8. 10.8. Arithmetic Expressions
        1. 10.8.1. Numeric Coercions
        2. 10.8.2. Casting Numeric Values
        3. 10.8.3. ~ Operator
      9. 10.9. Method Calls
        1. 10.9.1. Virtual Method Calls
        2. 10.9.2. Static Method Calls
        3. 10.9.3. Example of Method Calls
        4. 10.9.4. Overriding
        5. 10.9.5. Overloading
        6. 10.9.6. Interfaces
      10. 10.10. Expressions as Statements
      11. 10.11. Ifs and Booleans
        1. 10.11.1. Boolean Values
        2. 10.11.2. if Statements
        3. 10.11.3. Comparisons
        4. 10.11.4. Floating-Point Comparisons and NaN
        5. 10.11.5. Integer Comparisons
        6. 10.11.6. References and Object Equality
        7. 10.11.7. Boolean Operators
      12. 10.12. Other Control Structures
        1. 10.12.1. Loops
        2. 10.12.2. Compiling Control Constructs
        3. 10.12.3. Compound Statements and Verification
      13. 10.13. Returns
      14. 10.14. Field Declarations
      15. 10.15. Method Declarations
      16. 10.16. Constructors
        1. 10.16.1. Using Other Constructors in the Same Class
        2. 10.16.2. Default Constructors
        3. 10.16.3. Field Initializers and the Constructor
      17. 10.17. Conclusion
    13. 11. Compiling Other Languages
      1. 11.1. Java Version 1.1
        1. 11.1.1. Class Literals
        2. 11.1.2. Inner Classes
        3. 11.1.3. Variable Scoping with Inner Classes
      2. 11.2. Regular Expressions
      3. 11.3. Iterators
        1. 11.3.1. Iterators in Sather
        2. 11.3.2. Implementing Iterators
        3. 11.3.3. Iterators with Arguments
        4. 11.3.4. Using Iterators
      4. 11.4. Parameterized Types
        1. 11.4.1. Implementing Parameterized Types with a Generic Class
        2. 11.4.2. Implementing Parameterized Types as Templates
      5. 11.5. Multiple Inheritance
        1. 11.5.1. Interfaces for Multiple Inheritance
        2. 11.5.2. Implementing the Interfaces
        3. 11.5.3. Fields
      6. 11.6. Conclusion
    14. 12. Implementing Scheme
      1. 12.1. Scheme Concepts
      2. 12.2. Compiling Scheme into Bytecodes
      3. 12.3. Compiling Lambda Expressions
      4. 12.4. Example
      5. 12.5. Scheme Library
      6. 12.6. Optimizing Scheme
        1. 12.6.1. Static Scoping
        2. 12.6.2. Using Local Variables
        3. 12.6.3. Inlining
        4. 12.6.4. Tail Recursion
        5. 12.6.5. Using Type Information
    15. 13. Implementing Prolog
      1. 13.1. Introduction to Prolog
        1. 13.1.1. Asking Questions
        2. 13.1.2. Binding Patterns
        3. 13.1.3. Implications
        4. 13.1.4. Binding Patterns and Implications
        5. 13.1.5. Facts as Rules
        6. 13.1.6. More About Unification
      2. 13.2. Implementation Basics
      3. 13.3. Unification
        1. 13.3.1. Unification Algorithm
        2. 13.3.2. Managing Bindings
      4. 13.4. Rules as Programs
        1. 13.4.1. inside
        2. 13.4.2. within
        3. 13.4.3. yield and Backtracking
      5. 13.5. Implementing Rules
        1. 13.5.1. Implementing within
        2. 13.5.2. Implementing Conjunctions
        3. 13.5.3. Constructing within
      6. 13.6. Compiling Facts
      7. 13.7. Case Study
    16. 14. Performance
      1. 14.1. Fast Virtual Machines
        1. 14.1.1. Garbage Collection Performance
        2. 14.1.2. Implementing References
      2. 14.2. Bytecode Optimization Techniques
        1. 14.2.1. Warning
        2. 14.2.2. Constant Propagation
        3. 14.2.3. Strength Reduction
        4. 14.2.4. Loop Unrolling
        5. 14.2.5. Peephole Optimization
      3. 14.3. Inlining
        1. 14.3.1. Inlining Fields
        2. 14.3.2. Separate Compilation
    17. 15. Security and the Virtual Machine
      1. 15.1. Java Platform and Need for Security
      2. 15.2. Security Promises of the JVM
      3. 15.3. Security Architecture and Security Policy
        1. 15.3.1. Example
        2. 15.3.2. Basic Applet Security Policy
        3. 15.3.3. More Sophisticated Policies
      4. 15.4. Some Potential Attacks
        1. 15.4.1. Implementation Details
        2. 15.4.2. Protecting the Security Manager
        3. 15.4.3. Bypassing Java Security
        4. 15.4.4. Using Unconstructed Objects
        5. 15.4.5. Invalid Casts
        6. 15.4.6. Changing the Class of a Reference
        7. 15.4.7. Reading Uninitialized Fields
        8. 15.4.8. Array Bounds Checks
        9. 15.4.9. Catching Exceptions
        10. 15.4.10. Hidden Code
      5. 15.5. Conclusion
    18. 16. Threads and Synchronization
      1. 16.1. Threads
        1. 16.1.1. Example: Multithreaded Web Server
        2. 16.1.2. Exceptions and Threads
      2. 16.2. Sharing State Between Threads
        1. 16.2.1. Need for Synchronization
        2. 16.2.2. Exception: longs and doubles
      3. 16.3. Monitors and Object Locks
        1. 16.3.1. Synchronization in Java
        2. 16.3.2. Synchronized Methods
      4. 16.4. Scheduling
        1. 16.4.1. Surrendering Control of the CPU
        2. 16.4.2. Waiting for Another Thread
      5. 16.5. Conclusion
    19. A. Tables
      1. A.1. Instructions by Opcode
      2. A.2. Instructions Alphabetically
      3. A.3. Instructions by Category
        1. A.3.1. Arithmetic
        2. A.3.2. Array
        3. A.3.3. Constant
        4. A.3.4. Control
        5. A.3.5. Data Type Conversion
        6. A.3.6. Object
        7. A.3.7. Stack Manipulation
        8. A.3.8. Synchronization
        9. A.3.9. Variable
    20. B. Oolong Reference
      1. B.1. Using the Oolong Assembler
        1. Name
        2. Abbreviated name
        3. Synopsis
        4. Description
        5. Options
      2. B.2. Gnoloo
        1. Name
        2. Abbreviated name
        3. Synopsis
        4. Description
        5. Options
      3. B.3. DumpClass
        1. Name
        2. Abbreviated name
        3. Synopsis
        4. Description
      4. B.4. Oolong Language
        1. B.4.1. Comments
          1. Format
          2. Description
          3. Examples
        2. B.4.2. .source
          1. Format
          2. Description
          3. Examples
        3. B.4.3. .class
          1. Format
          2. Description
          3. Examples
        4. B.4.4. .interface
          1. Format
          2. Description
          3. Example
        5. B.4.5. .end class
          1. Format
          2. Description
          3. Example
        6. B.4.6. .super
          1. Format
          2. Description
          3. Examples
        7. B.4.7. .implements
          1. Format
          2. Description
          3. Examples
        8. B.4.8. .field
          1. Format
          2. Description
          3. Examples
        9. B.4.9. .method
          1. Format
          2. Description
          3. Examples
        10. B.4.10. .limit locals
          1. Format
          2. Description
          3. Examples
        11. B.4.11. .limit stack
          1. Format
          2. Description
          3. Examples
        12. B.4.12. .line
          1. Format
          2. Description
          3. Example
        13. B.4.13. .var
          1. Format
          2. Description
          3. Example
        14. B.4.14. .throws
          1. Format
          2. Description
          3. Examples
        15. B.4.15. .catch
          1. Format
          2. Description
          3. Examples
        16. B.4.16. .end method
          1. Format
          2. Description
          3. Examples
      5. B.5. Instructions
        1. B.5.1. Instructions with No Arguments
        2. B.5.2. Integers
        3. B.5.3. Labels
        4. B.5.4. Classes
        5. B.5.5. Fields and Methods
        6. B.5.6. invokeinterface
        7. B.5.7. multianewarray
        8. B.5.8. ldc, ldc_w, and ldc2_w
        9. B.5.9. newarray
        10. B.5.10. tableswitch
        11. B.5.11. lookupswitch
    21. C. Answers to Selected Exercises
    22. Further Reading
      1. Java and the JVM
      2. Newsgroup
      3. Other Languages
      4. Compilers