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:
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:
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? |
Part I: Object-Oriented Programming
Encapsulation
Polymorphism
Inheritance
Part II: Linear Structures
Stacks and Queues
Array-Based Structures
Linked Structures
Part III: Algorithms
Analysis of Algorithms
Searching and Sorting
Recursion
Part IV: Trees and Sets
Trees
Sets
Part V: Advanced Topics
Advanced Linear Structures
Strings
Advanced Trees
Graphs
Memory Management
Out to the Disk
Part VI: Appendices
A. Review of Java
B. Unified Modeling Language
C. Summation Formulae
D. Further Reading
Index