Chapter 11. Compiling Other Languages

One of the best uses for Java virtual machine programming is translating languages other than Java into virtual machine code. Compiled programs run faster than interpreted ones, and compilers are often easier to write than interpreters. By taking advantage of the JVM's built-in debugging features, you can even use standard debuggers for your new language.

There are many different kinds of languages. Some are complete in the sense that they are powerful enough to write programs for any computation. In this sense, Java, C, Lisp, and Basic are all complete. Completeness in this sense refers to the ability of a language to compute mathematical functions, not to its ability to manipulate hardware (such as graphics) or whether hooks are provided to a particular operating system feature.

Each of these languages includes features not included in the others, which gives each language a different flavor. A language designer carefully chooses the features to include in a language to make it powerful enough to write concise programs but not so complicated that it is hard to learn or hard to implement.

The Java language designers chose a set of features appropriate to general-purpose programming:

  • Class-based object-oriented design

  • Single inheritance of implementation (through the extends clause)

  • Multiple inheritance of abstract type (through the implements clause)

  • Virtual dispatch of method calls (through ordinary method invocation)

  • Nonvirtual dispatch of static calls (through static method invocation)

  • Static type checking (since each field and variable has a declared type)

  • Garbage collection mechanism to remove objects that are no longer used (provided by the JVM)

  • Object locking (through synchronized methods and the synchronized statement)

Other languages have features not included in Java:

  • Parametric polymorphism (C++ templates, Sather type parameters)

  • Closures (Lisp lambda expressions)

  • Multiple inheritance of implementation (C++, CLOS, or Eiffel multiple inheritance)

  • Weak type checking (Lisp, C)

  • Coroutines (Sather iterators, Scheme call-with-current-continuation)

It is possible to design a translator from any of these languages into JVM code. Sometimes the mapping from one language into JVM features is easy. For example, it is easy to see how the arithmetic instructions of the JVM instruction set can be used to implement numerical calculations of any language.

Other features, like pointer arithmetic in C++, are considerably harder though not impossible to implement using the JVM instructions.

A language does not have to be computationally complete to be useful. For example, regular expressions are widely used in search engines, although it is not possible to do general computation with them. Another example is SQL, the database query language, which is well suited to joining tables together but not for general-purpose calculations. The limited scope of these languages allows them to be better suited to a particular task than a general-purpose language like Java or C++.

This chapter suggests how some of these languages may be compiled into bytecodes and executed on any JVM implementation.

Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel © 2008-2017.
If you may any questions please contact us: