1.10 Sample Java Application

An application is what is normally called a program: source code that is compiled and directly executed. In order to create an application in Java, the program must have a class that defines a method called main. The main() method in the class is the starting point for the execution of any application.

Essential Elements of a Java Application

Example 1.4 is an example of an application in which a client uses the CharStack class to reverse a string of characters.

Example 1.4 An Application
// Source Filename: Client.java
public class Client {

    public static void main(String[] args) {

        // Create a stack
        CharStack stack = new CharStack(40);

        // Create a string to push on the stack
        String str = "!no tis ot nuf era skcatS";
        int length = str.length();
        System.out.println("Original string: " + str);

        // Push the string char by char onto the stack
        for (int i = 0; i<length; i++) {
            stack.push(str.charAt(i));
        }

        System.out.print("Reversed string: ");
        // Pop and print each char from the stack
        while (!stack.isEmpty()) {
            System.out.print(stack.pop());
        }
        System.out.println();
    }
}

// Source Filename: CharStack.java
public class CharStack {
    // Same as in Example 1.2.
}

Output from the program:

Original string: !no tis ot nuf era skcatS
Reversed string: Stacks are fun to sit on!

The public class Client defines a method with the name main. To start the application, the main() method in this public class is invoked by the Java interpreter, also called the Java Virtual Machine (JVM). The main() method should be declared as follows:

public static void main(String[] args) {
    // ...
}

The main() method has public accessibility, that is, it is accessible from any class. The keyword static means the method belongs to the class. The keyword void means the method does not return any value. The parameter list, String[] args, is an array of strings used to pass information to the main() method when the application is started.

Compiling and Running an Application

Java source files can be compiled using the Java compiler tool javac, which is part of the Java 2 SDK.

The source file Client.java contains the definition of the Client class. The source file can be compiled by giving the following input at the command line:

>javac Client.java

This creates the class file, Client.class, containing the Java byte code for the Client class. The Client class uses the CharStack class, and if the file CharStack.class does not already exist, the compiler will also compile the source file CharStack.java.

Compiled classes can be executed by the Java interpreter java, which is also part of the Java 2 SDK. Example 1.4 can be run by giving the following input at the command line:

>java Client

Note that only the name of the class is specified, resulting in starting the execution of the main() method from the specified class. The application in Example 1.4 terminates when the execution of the main() method is completed.