Section 2.1
The Basic Java Application


A PROGRAM IS A SEQUENCE OF INSTRUCTIONS that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form that the computer can use. This means that programs have to be written in programming languages. Programming languages differ from ordinary human languages in being completely unambiguous and very strict about what is and is not allowed in a program. The rules that determine what is allowed are called the syntax of the language. Syntax rules specify the basic vocabulary of the language and how programs can be constructed using things like loops, branches, and subroutines. A syntactically correct program is one that can be compiled or interpreted; programs that have syntax errors will be rejected (hopefully with a useful error message that will help you fix the problem).

So, to be a successful programmer, you have to develop a detailed knowledge of the syntax of the programming language that you are using. However, syntax is only part of the story. It's not enough to write a program that will run. You want a program that will run and produce the correct result! That is, the meaning of the program has to be right. The meaning of a program is referred to as its semantics. A semantically correct program is one that does what you want it to.

When I introduce a new language feature in these notes, I will explain both the syntax and the semantics of that feature. You should memorize the syntax; that's the easy part. Then you should try to get a feeling for the semantics by following the examples given, making sure that you understand how they work, and maybe writing short programs of your own to test your understanding. And note that even when you've become familiar with all the individual features of the language, that doesn't make you a programmer. You still have to learn how to construct complex programs to solve particular problems. For that, you'll need both experience and taste. You'll find hints about software development throughout the notes.


We begin our exploration of Java with the problem that has become traditional for such beginnings: to write a program that displays the message ""Hello World!". This might seem like a trivial problem, but getting a computer to do this is really a big first step in learning a new programming language (especially if it's your first programming language). It means that you understand the basic process of:

  1. getting the program text into the computer,
  2. compiling the program, and
  3. running the compiled program.

The first time through, each of these steps will probably take you a few tries to get right. I won't tell you the details here of how you do each of these steps; it depends on the particular computer and Java programming environment that you are using. But in general, you will type the program using some sort of text editor and save the program in a file. Then, you will use some command to try to compile the file. You'll either get a message that the program contains syntax errors, or you'll get a compiled version of the program. In the case of Java, the program is compiled into Java bytecode, not into machine language. Finally, you can run the compiled program by giving some appropriate command. For Java, you will actually use an interpreter to execute the Java bytecode. (Your programming environment might automate some of the steps for you, but you can be sure that the same three steps are being done in the background.)

Here is a Java program to display the message "Hello World!". Don't expect to understand what's going on here just yet -- some of it you won't really understand until a few chapters from now:

           public class HelloWorld {
            
              // A program to display the message
              // "Hello World!" on standard output

              public static void main(String[] args) {
                 System.out.println("Hello World!");
              }
                 
           }   // end of class HelloWorld

The command that actually displays the message is:

           System.out.println("Hello World!");

This command is an example of a subroutine call statement. It uses a "built-in subroutine" named System.out.println to do the actual work. Recall that a subroutine consists of the instructions for performing some task, chunked together and given a name. That name can be used to "call" the subroutine whenever that task needs to be performed. A built-in subroutine is one that is already defined as part of the language and therefore automatically available for use in any program.

When you run this program, the message "Hello World!" (without the quotes) will be displayed on standard output. Unfortunately, I can't say exactly what that means! Java is meant to run on many different platforms, and standard output will mean different things on different platforms. However, you can expect the message to show up in some convenient place.

You must be curious about all the other stuff in the above program. Part of it consists of comments. Comments in a program are entirely ignored by the computer; they are there for human readers only. This doesn't mean that they are unimportant. Programs are meant to be read by people as well as by computers, and without comments, a program can be very difficult to understand. Java has two types of comments. The first type, used in the above program, begins with // and extends to the end of a line. The computer ignores the // and everything that follows it on the same line. Java has another style of comment that can extend over many lines. That type of comment begins with /* and ends with */.

Everything else in the program is required by the rules of Java syntax. Every program must define a subroutine called main, with a definition that takes the form:

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

The word public here means that this subroutine can be called from outside the program. This is essential because the Java interpreter actually runs a program by calling its main subroutine. The remainder of the first line is harder to explain at the moment; for now, just think of it as part of the required syntax. The definition of the subroutine -- that is, the instructions that say what it does -- consists of a sequence of "statements" enclosed between braces, { and }. Here, I've used statements as a placeholder for the actual statements. I will always use this format: anything that you see in bold text (and in green if your browser supports colored text) is a placeholder that describes what you need to type when you write an actual program.

In Java, a subroutine can't exist by itself. It has to be part of a "class". The main subroutine must be part of a public class that looks like this:

            public class program-name {
            
                optional-variable-declarations-and-subroutines
                
                public static void main(String[] args) {
                   statements
                }
                
                optional-variable-declarations-and-subroutines
          
            }

Note that the name on the first line is the name of the program as well as the name of the class. Thus, the name of the hello world program given above is HelloWorld. This program should be saved in a file called HelloWorld.java, and when it is compiled, a file named HelloWorld.class will be produced. The resulting class file, HelloWorld.class, contains Java bytecode that can be executed by a Java interpreter.

Also note that a program can contain other subroutines besides main() as well as things called "variable declarations." You'll learn more about these later (starting with variables, in the next section).


By the way, recall that one of the neat features of Java is that is can be used to write applets that can run on pages in a Web browser. Applets are very different things from programs, and they are not written in the same way. For one thing, an applet doesn't have a main() routine. You won't start learning about programming applets until Chapter 4. However, in the meantime, I will use applets to simulate programs. They won't really be programs, since they run right on a Web page, but they will have the same behavior as the programs I describe. Here, just for fun, is an applet simulating the HelloWorld program:

Sorry, your browser doesn't
support Java.


[ Next Section | Previous Chapter | Chapter Index | Main Index ]