Home > Store

Programming for the Java Virtual Machine

Register your product to gain access to bonus material or receive a coupon.

Programming for the Java Virtual Machine


  • Sorry, this book is no longer in print.
Not for Sale


  • Copyright 1999
  • Dimensions: 7-3/8" x 9-1/4"
  • Pages: 512
  • Edition: 1st
  • Book
  • ISBN-10: 0-201-30972-6
  • ISBN-13: 978-0-201-30972-0

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.



CD Contents

Download the CD Contents related to this title.

Sample Content

Table of Contents


1. Introduction to the Java Virtual Machine.

Java Virtual Machine and Java.

What Is the Java Virtual Machine?

Instruction Set.

class File Format.


Java Platform.

Organization of the Java Virtual Machine.

Class Area.

Java Stack.


Native Method Stacks.


Garbage Collection.


Timing of Garbage Collection.

JVM Implementations.

Versions of the Virtual Machine.

Future of the Java Virtual Machine.

2. Oolong.

Hello, World.

.class and .super Directives.

main Method.


Structure of an Oolong Program.

Defining the Class.


Declaring Fields.

Declaring Methods.


3. Writing Methods.

Some Definitions.


Numeric Types.

Object Types.

Type returnAddress.

Mnemonic Naming Conventions.

Testing Code Examples.



General Constants.

Small Constants and null.

Local Variables.

Initializing Variables.

Local Variables as Method Arguments.


Arithmetic Operations.

Nonnumbers and Infinity.

Bitwise Operations.

Floating-Point Arithmetic and strictfp.

Type Conversions.

Conversion Example.

Widening and Narrowing.

Stack Manipulations.

Arrays of Numbers.

Just Biding Your Time.

4. Classes and Objects.

What Is an Object?

Objects and References.

Class Names.


Creating Objects.


Using Fields.

Types and getfield.

Inheriting Fields.

Changing Field Values.

Invoking Methods on Objects.

Virtual Invocation.

Method Inheritance and Overriding.

Overloading and Method Matching.

Invoking a Method Directly.

invokespecial and super.


Casting, Fields, Methods, and Java.

Returning Objects.


Static Fields and Methods.

Class Initialization.


Arrays of Reference.

Multidimensional Arrays.

Length of Arrays.

5. Control Instructions.

Comparing Integers.

Comparing References.

Other Comparisons.




Multiple Exception Handlers.

.throws Directive.

Other Ways to Throw Exceptions.

A Gentler Way of Checking Type.

6. Verification Process.

How the Verification Algorithm Works.

Is It a Structurally Valid class File?

Are All Constant References Correct?

Will Each Instruction Always Find a Correctly Formed Stack and Local Variable Array?

Example 1: Hello, World.

Example 2: gotos and ifs.

Example 3: Code That Doesn’t Verify.

Example 4: Dealing with Subclasses.


Do External References Check Out?

Java Language and Verification Algorithm.

Fooling the Virtual Machine with a Java Compiler.

Other Languages and the Verification Algorithm.

Other Safety Requirements.

Checking Verification.

7. Debugging.

Debugging Directives in Oolong.

.source Directive and .line Directives.

.var Directive.

Debugging Oolong Programs.

Runtime Tracing.

8. Class Loaders.

How Classes Are Loaded.



Loading Classes without a Class Loader.

Making Your Own Class Loader.

Caching Classes.


Working with Class Objects.


9. Inside a Java class File.

class File Header.

Constant Pool.

UTF8 Entries.

Constant Entries.

Field, Method, and Class Entries.

Class Information.

Fields and Methods.

Class Attributes.

Other Attributes.

ConstantValue Attribute.

Exceptions Attribute.

InnerClasses Attribute.

10. Compiling Java.

Expressions and Statements.

Expression Types.

Numeric Types.

Object Types.

Expression Result Types.

Compile-Time Types and Type Restrictions.

Type Safety and Verifiability.

Compiling Statements and Expressions.

Local Variable Declarations.

Fields and Variables.


Other Assignments.

Assignments as Expressions.

Type-Checking in Assignments.

Creating New Objects.

Class Name Abbreviations.

Arithmetic Expressions.

Numeric Coercions.

Casting Numeric Values.


Method Calls.

Virtual Method Calls.

Static Method Calls.

Example of Method Calls.




Expressions as Statements.

Ifs and Booleans.

Boolean Values.

if Statements.


Floating-Point Comparisons and NaN.

Integer Comparisons.

References and Object Equality.

Boolean Operators.

Other Control Structures.


Compiling Control Constructs.

Compound Statements and Verification.


Field Declarations.

Method Declarations.


Using Other Constructors in the Same Class.

Default Constructors.

Field Initializers and the Constructor.


11. Compiling Other Languages.

Java Version 1.1.

Class Literals.

Inner Classes.

Variable Scoping with Inner Classes.

Regular Expressions.


Iterators in Sather.

Implementing Iterators.

Iterators with Arguments.

Using Iterators.

Parameterized Types.

Implementing Parameterized Types with a Generic Class.

Implementing Parameterized Types as Templates.

Multiple Inheritance.

Interfaces for Multiple Inheritance.

Implementing the Interfaces.



12. Implementing Scheme.

Scheme Concepts.

Compiling Scheme into Bytecodes.

Compiling Lambda Expressions.


Scheme Library.

Optimizing Scheme.

Static Scoping.

Using Local Variables.


Tail Recursion.

Using Type Information.

13. Implementing Prolog.

Introduction to Prolog.

Asking Questions.

Binding Patterns.


Binding Patterns and Implications.

Facts as Rules.

More About Unification.

Implementation Basics.


Unification Algorithm.

Managing Bindings.

Rules as Programs.



yield and Backtracking.

Implementing Rules.

Implementing within.

Implementing Conjunctions.

Constructing within.

Compiling Facts.

Case Study.

14. Performance.

Fast Virtual Machines.

Garbage Collection Performance.

Implementing References.

Bytecode Optimization Techniques.


Constant Propagation.

Strength Reduction.

Loop Unrolling.

Peephole Optimization.


Inlining Fields.

Separate Compilation.

15. Security and the Virtual Machine.

Java platform and Need for Security.

Security Promises of the JVM.

Security Architecture and Security Policy.


Basic Applet Security Policy.

More Sophisticated Policies.

Some Potential Attacks.

Implementation Details.

Protecting the Security Manager.

Bypassing Java Security.

Using Unconstructed Objects.

Invalid Casts.

Changing the Class of a Reference.

Reading Uninitialized Fields.

Array Bounds Checks.

Catching Exceptions.

Hidden Code.


16. Threads and Synchronization.


Example: Multithreaded Web Server.

Exceptions and Threads.

Sharing State Between Threads.

Need for Synchronization.

Exception: longs and doubles.

Monitors and Object Locks.

Synchronization in Java.

Synchronized Methods.


Surrendering Control of the CPU.

Waiting for Another Thread.


A. Tables.

Instructions by Opcode.

Instructions Alphabetically.

Instructions by Category.





Data Type Conversion.


Stack Manipulation.



B. Oolong Reference.

Using the Oolong Assembler.



Oolong Language.





end class.





limit locals.

limit stack.





end method.


Instructions with No Arguments.




Fields and Methods.



ldc, ldc_w, and ldc2_w.




C. Answers to Selected Exercises.

Further Readings.

Java and the JVM.


Other Languages.


Index. 0201309726T04062001


Every time you compile a Java program to produce a class file, you've written a program for the Java virtual machine. This book is designed to take you to the next level: writing Java virtual machine (JVM) programs directly, without the aid of a Java compiler. A number of people would want to do this:

  • Advanced Java programmers
  • Security experts
  • Language designers and compiler writers

Advanced Java programmers want to know how the JVM works, in detail. Learning the details of the JVM will help improve your understanding of what a Java program does. Some messages from the JVM will be clearer after you understand how the JVM looks at your program. This understanding can also help you improve the performance of your programs.

Another reason for advanced Java programmers to learn JVM programming is that it will help you understand how JVM code moves. One of the most important uses of Java is to download applets from the Internet (or other sources). The JVM uses a class called ClassLoader to incorporate code from the Internet into the JVM. The ClassLoader has many other uses. The Java Foundation Classes use subclasses of ClassLoader to load new Look and Feel classes. Some databases use other subclasses of ClassLoader to incorporate queries written in Java directly into the database.

Security experts already know that the Java language is designed with security features in mind. Java promises to allow users to download applets from the Internet without fear. However, these promises seem empty until you know precisely how the JVM keeps the promises Java makes. This book discusses in detail how the JVM verification algorithm works. Knowing the verification algorithm will give you greater confidence in the security of the JVM.

This book will give you the tools you need to build your own secure systems on top of the JVM, by explaining both what promises the JVM makes (and how it keeps them) and what promises it doesn't make.

Language designers want to design new languages for the Java virtual machine. Although the Java language is excellent, perhaps the best general-purpose programming language ever created, there are still times when a different language does the job better. Sometimes these are general-purpose languages built into an application (like WordBasic in Microsoft Word or Emacs Lisp). Others are tiny, special-purpose languages (like SQL, regular expressions, or filters in PhotoShop).

This book describes compilers for two languages, Scheme and Prolog, which are very different from Java. These compilers generate JVM code and use a subclass of ClassLoader to load it into the system. By compiling into JVM code, these non-Java languages gain some of the "Write Once, Run Anywhere" properties of Java without losing the power of the original language. These compilers can be used as a starting point for compiling other languages. Other languages already implemented for the JVM include Tcl, ML, Eiffel, Python, and Ada.

As more and more programs are written in Java, there is an increasing need to implement new languages. By implementing these languages using the JVM, you can get the portability and performance of the JVM.


This book assumes that you have a good basic knowledge of Java. Many of the ideas in the book are explained in terms of Java; at the same time, the book tries to explain Java in terms of the JVM. Hopefully, this will reinforce your understanding of both the Java language and the Java virtual machine.

If you're new to Java, let me recommend The Java Programming Language, by Ken Arnold and James Gosling. It's an excellent introduction to the language written in part by the originator of Java himself, James Gosling.

Many sections have exercises. The exercises don't introduce new material, but they do reinforce the existing lessons. Most of the exercises ask you to write small pieces of code. For some exercises, answers are provided at the back of the book. If you're using this book for a class and you've been assigned the exercises as a homework problem, then turning in the answers at the back of the book is cheating! Fortunately, real JVM programmers are trustworthy.


This book has been designed to stand on its own. You should be able to read it without owning any other books on Java. However, there are two books which you may find very useful.

One is The Java Virtual Machine Specification (JVMS), by Frank Yellin and Tim Lindholm. It is the "truth" of the Java virtual machine. All JVM implementers, including Sun itself, are expected to adhere to the definition of the Java virtual machine contained in the JVMS. Wherever your JVM implementation disagrees with the JVMS, it is always the JVMS that is correct. I recommend that you have a copy on hand if you want to do a lot of JVM programming, because it is the final word on any JVM-oriented question.

Another book I recommend is The Java Language Specification, by James Gosling, Bill Joy, and Guy Steele. This is a large, thick tome that contains the absolute truth about the Java language. It does an excellent job of specifying all the details of what a Java program means. It is meticulously precise, but it can still be a lot of fun to read because it throws in many funny examples and quotes. Even just browsing the index can be a treat. (Look up the index entry for prime numbers to see what I mean.)

Both of these books are available over the Internet at http://java.sun.com/docs. Paper copies are available at any bookstore.

While you're on the Internet, let me recommend one other source of information. The newsgroup comp.lang.java.machine is dedicated to discussing how the Java virtual machine works, how it is implemented, and so forth. I'm a frequent reader and contributor, and there are many other experts willing to share their copious knowledge.


I would like to thank Stefan Gower for giving me the original idea of implementing a Scheme compiler for the Java virtual machine. Christopher Cook, William Andersen, Brian Peterson, and Paul Brinkley were indispensable, allowing me to explain JVM concepts to them until I finally found a lucid explanation.

Marina Lang at Addison Wesley Longman was instrumental in guiding me through the process of getting a book written. Thanks also to the many reviewers she found who helped me turn drafts into something readable.

Finally, I would like to thank the regular posters to comp.lang.java.machine, the newsgroup for the JVM. They provided answers to a lot of questions and brought up questions I had not previously considered.


More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020