A.1. The First Program
When a computer scientist sets out to learn a new programming language, one of the first steps is to write a "Hello, world" program. Among other things, this ensures that the compiler and other development tools have been installed correctly.
Before you can run this program, you will need to install a Java system such as Sun's J2SE (Java 2, Standard Edition) Software Development Kit, available for free at java.sun.com. The code in this book was tested on version 1.5.0 and should work with any later version. (Confusingly, version 1.5.0 is also known as version 5.0.) You can check your Java version with the command:
java -version
Our first program is shown in Figure A-1.
Figure A-1. The Hello program.
1 /** The standard "Hello, world" program. */ 2 public class Hello { 3 4 /** Print a friendly greeting. */ 5 public static void main(String[] args) { 6 System.out.println("Hello, world!"); 7 } 8 9 } |
Type it into a file called Hello.java, save it, and compile it with the command:
javac Hello.java
Finally, run the program with the command:
java Hello
Some common bugs to watch out for:
- The program must be saved in a file called Hello.java. This is case sensitivehello.java is not good enough.
- The command to compile is javac, but the command to run is java.
- javac needs an extension (.java) for the filename, but java does not. (In fact, java won't accept a filename with an extension.)
Let's examine the program line by line. (The line numbers are only for our convenience when discussing programsdon't type them.)
Line 1 is a comment, as is line 4. Comments make the program more readable to humans. They are ignored by the Java system.
Line 2 names the program Hello. The program must be saved in an appropriately named file. The keyword public is discussed in Section 3.3. Classes are covered in Chapter 1. The curly brace at the end of the line opens a block of code which ends at the matching brace on line 9. The entire program must be within this block.
This program has one method (similar to a function or procedure in another language). This is the main() method. When we run a class, we are really running the main() method. The method occupies lines 5 through 7.
Line 5 is the signature of the method, indicating various aspects of the way the method behaves. The signature includes the following facts:
- The method is public, meaning that any program can see it. More on this in Section 3.3.
- The method is static. All methods in this appendix are static. We will explain what this means, and introduce nonstatic methods, in Chapter 1.
- The method does not return a value. Put another way, the return type of the method is void. Some methods return values. Others, like this one, only print things or have other side effects. Still other methods both have side effects and return values.
- The name of the method is main(). When we refer to method names in the text, we will include empty parentheses behind the name to emphasize that it is a method, rather than a variable or something else.
- The method takes one argument, which is of type String[] and named args. We ignore this argument here, but it is necessary because the main() method always has the same signature. Programming language theorists distinguish between a method's parameters (its own names for the values it receives) and its arguments (the values themselves). We will ignore this distinction and always refer to them as arguments.
Two other things might appear in a method signature: a list of exceptions thrown (Section 4.3) and any generic type parameters (Section 4.1).
The body of the method is line 6. This is the part that actually does something! It invokes another method by passing an argument to it. The method is called System.out.println(). More precisely, the method is named println(), and System.out indicates where the method can be found. This will be explained in more detail in Section A.7.
Not surprisingly, this method prints out its argument, followed by a newline character.
Exercises
A.1 |
Change the message in the Hello program, recompile it, and run it again. |
A.2 |
What error message do you get if you leave off the closing curly brace? |
A.3 |
What error message do you get if the method is outside of the classthat is, if you move the final curly brace from line 9 to line 3? |
A.4 |
What error message do you get if you save the program as Howdy.java? |