Friday, December 7, 2007

Java Programs

A Java program is a collection of one or more classes, with one of them containing the program's execution starting point. A Java source file can contain more than one class definition. The Java 2 SDK enforces the rule that at the most one class in the source file has public accessibility. The name of the source file is comprised of the name of this public class with .java as extension. Each class definition in a source file is compiled into a separate class file, containing Java byte code. The name of this file is comprised of the name of the class with .class as an extension. All programs must be compiled before they can be run. The Java 2 SDK provides tools for this purpose, as explained in 1.10 Sample Java Application.

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 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();
}
}


}

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.

No comments: