Section 16.4 - Java and Ada

Sun's Java technology has become a "hot" topic. Java makes it possible for users to run programs just by browsing the World Wide Web (WWW). Using Java, WWW users can use sophisticated user interfaces and handle arbitrary data formats (the data and the program to handle the data can be sent together!). Java can also be used to distribute computer platform independent software (i.e. the same software would run on an IBM-compatible PC, Apple Macintosh, and arbitrary Unix machine).

It turns out that Java programs can be created using Ada. To understand what that means, we'll first need to define some terms.

Java Terminology

You can use Java to develop applications and applets:

  1. An application is a traditional kind of program. Users install Java applications in a manner similar to any other application, and a Java application can read files, write files, and so on. It's possible for a single Java application to run on many different computer platforms.
  2. An applet is a program that automatically starts running when a WWW user views a page containing the applet. The user does not install the applet; in fact, the user isn't even asked if he or she wants the applet to run. To keep this from becoming a security problem, applets are restricted from performing certain tasks. At this time, applets generally can't read or write to local disk files, they can't print, and they can only communicate over a network to the computer where they came from. In some cases users may grant their applets additional privileges; the key is that applets are normally restricted to keep them becoming a security problem.

The Java Technology, as developed by Sun, can be divided into four components:

  1. Specifications for the Java Virtual Machine (JVM) and class files. The JVM is an abstract computer that executes programs stored in "class" files. The JVM can be implemented on real computers in many different ways, and that's the point: as long as your computer faithfully recreates this abstract computer, it can run programs stored in class files. For example, the JVM might be implemented as an interpreter built into a web browser, or as a separate program that interprets the class files. Your computer could implement the JVM by transforming the class files into an executable program specific for that machine just before running them (this is called a "just-in-time" compiler). In fact, your computer hardware might even use JVM directly. As long as you have an implementation of the JVM, you can run Java programs, because Java programs are stored in class files. Class files are also called J-code files.
  2. The Java language. The Java language is an object-oriented computer programming language that resembles C++ and Objective-C in syntax. It resembles Ada in its emphasis on safety (for example, neither have pointers), and a strong Smalltalk influence is evident as well.
  3. A compiler that generates class files. The JVM runs class files, so you need a way to create them. Sun has developer a compiler that takes programs written in the Java language and generates Java class files. Other vendors have also developed compilers that generate class files.
  4. The Java library. The Java technology includes a set of components for simple platform-independent graphical user interface (GUI) handling as well as other useful components.

Many people use the term "Java" for each of these different components and for the technology as a whole. You'll need to determine what they mean by its context. The key point is that when people "run a Java program", they're actually running a set of class files on their version of the JVM.

An Ada compiler that accepts Ada code as its input and generates Java class files as its output makes it possible to generate Java programs using Ada. Intermetrics' Ada compiler, AppletMagic, does this, and other Ada compilers may follow.

Since programs only know about each other through their Java class files, programs written in the Java language and Ada language can freely communicate with each other. Java programs can easily call Ada programs just by looking at their class files. To permit Ada programs to call existing Java programs, Ada programs need an Ada specification. AppletMagic includes a tool called java2ada that generates Ada specifications from class files, and any other Ada-to-Java compilers would probably include similar tools.

[Relationship of Java Technology Components]

Java-Ada Correspondence

Many concepts in Ada and Java are quite close. Java's "primitive data types" generally have simple corresponding Ada types: Java "boolean" corresponds to Ada "Boolean", Java "float" to Ada "Float", Java "char" to Ada "Wide_Character", and Java "int" to Ada "Integer". All other Java data types are passed by reference, which corresponds to passing around Ada's access types. Both Ada and Java support hierarchical packages.

The Java library is a critical part of Java technology, so standard conventions are needed to define how an Ada program can call a Java library component. That way, when you read about a Java library component in a Java book, you can easily determine how to call it from Ada. The convention used by AppletMagic is that a Java class "C" in Java package "P" is translated to an Ada package named "P.C". Inside that Ada package is an Ada tagged type named C_Obj and an access type named C_Ptr. Java methods that return nothing (have "void" in front of their method name) become Ada procedures, while Java methods that return something (i.e. have some type name in front of the method name) usually become Ada functions. Java methods, unless they're defined as "static", have an implicit initial parameter identifying the object being handled; this translates to an Ada subprogram with an additional first parameter of type "access CLASS_BEING_DEFINED_Obj". All other parameters have the corresponding Ada type if they're a Java primitive type or CLASS_NAME_Ptr if they're a Java class.

For example, here's an abbreviated definition of Java class "Applet" in Java package "java.applet". Don't be confused by the use of the phrase "applet" in two different ways; Java package "java.applet" contains a number of classes, including the class "Applet". In the Java language, class Applet is defined as:

 package java.applet;
 public class Applet extends Panel {
   public void init();  // initialize the Applet.
   public boolean IsActive();
   public void resize(int width, int height);
   public Image getImage(URL url);
   public void showStatus(String msg);

This is translated into the following Ada package:

 with java.awt.Panel; use java.awt.Panel;  -- Package with Parent Type.
 with java.lang.String; use java.lang.String;  -- Java Strings.
 with; use;

 package java.applet.Applet is
  type Applet_Obj is new Panel_Obj with null record;
  type Applet_Ptr is access all Applet_Obj'Class;

  procedure init(Obj : access Applet_Obj); -- initialize the Applet.
  function isActive(Obj : access Applet_Obj) return Boolean;
  procedure resize(Obj : access Applet_Obj; width : Integer; height : Integer);
  function getImage(Obj : access Applet_Obj; url : URL_Ptr) return Image_Ptr;
  procedure showStatus(Obj : access Applet_Obj; msg : String_Ptr);
 end java.applet.Applet;

Now that you know how they correspond, you can call or override the Java library routines from Ada. I suggest that you try to use the same conventions for your own packages if you're writing Java programs in Ada; while in many cases it's not necessary, it makes your program more uniform and easier to understand. The correspondence described here is from AppletMagic, but it's reasonable to expect that any other Ada compiler that generates Java would use the same conventions (these conventions were defined by the designer of Ada 95!).

More specific details of the correspondence between Java and Ada, including information on constructors, is described in a paper by Tucker Taft [1996]. Documentation on the Ada interface to the Java library is available. More Ada/Java information can be found at the Home of the Brave Ada Programmers' Java section and the SIGAda Web Working group page. As noted above, information about Intermetrics' AppletMagic is available as well. More information about Java in general can be found in the Java FAQ and Sun's Java site. A large collection of sample Java applets is available at Gamelan.


Let's say that you want to write a program that is automatically run when a user views a page on the World Wide Web (WWW). Which of the following kind of program do you want to create?

  1. Application
  2. Applet

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 16 outline

David A. Wheeler (

The master copy of this file is at "".