Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Course Notes
  


Applications page 4 of 37


With the Java(TM) class and method syntax in hand, we can design a Java program. Java applications consist of one or more classes that define data and behavior. Java applications are translated into a distilled format by a Java compiler. This distilled format is nothing more than a linear sequence of operation-operand(s) tuples:

<operation><operand>...
<operation><operand>...
......
<operation><operand>...

This stream of data is often called a bytecode stream, or simply Java bytecodes. The operations in the bytecode stream implement an instruction set for a so-called virtual machine (software-based instruction processor), commonly called a Java virtual machine (JVM). Programs that implement the JVM simply process Java class files, sometimes specific to a particular environment. For example, Java-enabled Web browsers such as Netscape Navigator and Internet Explorer include a JVM implementation. Standalone programs that implement the JVM are typically called Java interpreters.

The Java compiler stores this bytecode stream in a so-called class file with the filename extension .class. Any Java interpreter can read/process this stream--it "interprets" each operation and its accompanying data (operands). This interpretation phase consists of (1) further translating the distilled Java bytecodes into the machine instructions for the host computer and (2) managing the program's execution. The following diagram illustrates the compilation and execution processes:

Java class files are portable across platforms. Java compilers and interpreters are typically not portable; they are written in a language such as C and compiled to the native machine language for each computer platform. Because Java compilers produce bytecode files that follow a prescribed format and are machine independent, and because any Java interpreter can read and further translate the bytecodes to machine instructions, a Java program will run anywhere--without recompilation.

A class definition such as Dog is typically stored in a Java source file with a matching name, in this case, Dog.java. A Java compiler processes the source file producing the bytecode class file, in this case, Dog.class. In the case of Dog, however, this file is not a Java program.

A Java program consists of one or more class files, one of which must define a program starting point--Dog.class does not. In other words, this starting point is the difference between a class such as Dog and a class definition that implements a program. In Java, a program's starting point is defined by a method named main(). Likewise, a program must have a well-defined stopping point. In Java, one way to stop a program is by invoking/executing the (system) method exit().

So, before we can do anything exciting, we must have a program that starts and stops cleanly. We can accomplish this with an arbitrary, user-defined data type that provides the main() and exit() behavior, plus a simple output operation to verify that it actually works:


public class SimpleProgram {
  public static void main(String[] args) {
    System.out.println("This is a simple program.");
    System.exit(0);
  }
}

The signature for main() is invariable; for now, simply define a program entry point following this example--with the modifiers public and static and the return type void. Also, System (java.lang.System) is a standard class supplied with every Java environment; it defines many utility-type operations. Two examples are illustrated here: (1) displaying data to the standard output device (usually either an IDE window or an operating system command window) and (2) initiating a program exit.

Note that the 0 in the call to exit() indicates to the calling program, the Java interpreter, that zero/nothing went wrong; that is, the program is terminating normally, not in an error state.

At this point, we have two class definitions: one, a real-world, user-defined data type Dog, and the other, a rather magical class that connects application-specific behavior with the mechanics of starting and stopping a program.

Now is a good time to get acquainted with your Java development environment. If you have an integrated development environment (IDE), it may or may not be file-oriented. With most environments Java source code is stored in a file. One popular exception is IBM's VisualAge for Java, which stores class definitions in a workspace area.

When using an IDE that is file-oriented, note that the filenames and class names must match exactly; in particular, the file and class names are case sensitive. Also, you must work within the rules established for a Java environment with respect to system environment variable settings, and so on. The panel Run-time environments and class path settings includes general information on system settings.

The first exercise is simple but important, because it tests your Java configuration. It includes these basic steps:

  • Write SimpleProgram exactly as shown here
  • Save it as required by the IDE
    • Somewhere in the IDE workspace environment
    • Or, in a separate file named SimpleProgram.java depending on the IDE
  • Build the program
  • Execute it
  • Observe the output

Exercises occur throughout the course; simply follow the links.

Exercise

copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact