Understanding How Java Programs Work

Understanding How Java Programs Work


An important distinction to make in Java programming is where your program is supposed to be running. Some programs are intended to work on your computer; you type in a command or click on an icon to start them up. Other programs are intended to run as part of a World Wide Web page. You encountered several examples of this type of program during the previous hour's whirlwind vacation.

Java programs that run locally on your own computer are called applications. Programs that run on Web pages are called applets. During this hour, you'll learn why that distinction is important, and the following topics will be covered:

·         How applications work

·         Organizing an application

·         Sending arguments to an application

·         How applets work

·         The required parts of an applet

·         Sending parameters to an applet

·         Using HTML tags to put an applet on a page


Creating an Application


Although Java has become well-known because it can be used in conjunction with World Wide Web pages, you can also use it to write any type of computer program. The BigDebt program that you wrote during Hour 2, "Writing Your First Program," is an example of a Java application.

To try out another program, use your word processor to open up a new file and enter everything from Listing 4.1. Remember not to enter the line numbers and colons along the left-hand side of the listing; these items are used to make parts of programs easier to describe in the book. When you're done, save the file as Root.java.

Listing 4.1. The full text of Root.java.

 

 1: class Root {

 2:         public static void main(String[] arguments) {

 3:                 int number = 225;

 4:                 System.out.println("The square root of "

 5:                         + number

 6:                         + " is "

 7:                         + Math.sqrt(number) );

 8:         }

 9: }


Before you can test out this application, you need to compile it with the javac compiler tool. While in the same directory as the Root.java file, compile it with the following command:

javac Root.java

If you have entered Listing 4.1 without any typos, including all punctuation and every word capitalized as shown, it should compile without any errors. The javac compiler responds to a successful compilation by not responding with any message at all.

You run Java applications in the same way you would run any program that's installed on your computer. Because they require the use of the java interpreter to run, the most common way that you'll run a Java program is probably by typing a command like the following at a command-line prompt:

java DrumMachine

This command would cause the java interpreter to look for a Java program called DrumMachine.class in the current directory. If it found one, it would start running it. To run the Root application, type the following:

java Root

The output should resemble the following:

The square root of 225 is 15.0

When you run a Java application, the interpreter looks for a main() block and starts handling Java statements at that point. If your program does not have a main() block, as most applets do not, the interpreter will respond with an error.

Sending Arguments to Applications

Because Java applications are usually run from a command line, you can send information to applications at the same time that you run them. The following example uses the java interpreter to run an application called DisplayTextFile.class, and it sends two extra items of information to the application: readme.txt and /p:

java DisplayTextFile readme.txt /p

The extra information you can send to a program is called arguments. The first argument, if there is one, is provided one space after the name of the application. Each additional argument is also separated by a space. You can send as many arguments as you want to a Java application. In order to do something with them, however, you have to write some statements in the application to handle them.

To see how arguments work in an application, create a new file in your word processor called NewRoot.java. Enter the text of Listing 4.2 into the file and save it when you're done. Compile the program with the javac compiler tool, while correcting any errors that are caused by typos.

Listing 4.2. The full text of NewRoot.java.

 

 1: class NewRoot {

 2:         public static void main(String[] arguments) {

 3:                 int number = 0;

 4:                 if (arguments.length > 0)

 5:                         number = Integer.parseInt( arguments[0] );

 6:                 System.out.println("The square root of "

 7:                         + number

 8:                         + " is "

 9:                         + Math.sqrt(number) );

10:         }

11: }


This program is similar to the Root program except for Lines 3-5. Don't worry about the specific statements used in these lines; they use some advanced features. What's important to note is what these lines are accomplishing: If an argument is sent to the NewRoot program when it is run, the argument is stored in the number variable.

To try the program out, use the Java interpreter with a command such as the following:

java NewRoot 169

This command causes the output to report that the square root of 169 is 13.0. Try the program several times with different numbers.

Arguments are a useful way to customize the performance of a program. They are often used to configure a program so that it runs a specific way. Java applications use arguments, but applets use a different way to receive information as they are run.

Post a Comment

Previous Post Next Post