Previous | Table of Contents | Next |
1This chapter is reprinted with the permission of Sun Microsystems from Arnold, K., and J. Gosling. 1996. The Java Programming Language. Reading, MA: Addison-Wesley.
by Ken Arnold and James Gosling
See Europe! Ten Countries in Seventeen Days!
Sign in a travel agents window
This chapter is a whirlwind tour of the Java programming language that gets you started writing Java code quickly.2 We cover the main points of the language quickly, without slowing you down with full-blown detail.
2The Java programming language is written simply as Java throughout.
Java programs are built from classes. From a class definition, you can create any number of objects that are known as instances of that class. Think of a class as a factory with blueprints and instructions to build gadgetsobjects are the gadgets that factory makes.
A class contains two kinds of members, called fields and methods. Fields are data belonging either to the class itself or to objects of the class; they make up the state of the object or class. Methods are collections of statements that operate on the fields to manipulate the state.
The first sample program in many languages prints Hello, world. Here is the Java version:
class HelloWorld { Public static void main (String[] args) { System.out.println(Hello, world); } }
Use your favorite text editor to type this program source into a file. Then run the Java compiler to compile the source of this program into Java bytecodes, the machine language for the Java virtual machine. Details of editing source and compiling vary from system to system and arent described hereconsult your system manuals for specific information. When you run the program, it displays:
Hello, world
Now you have a small Java program that does something, but what does it mean?
The program above declares a class called HelloWorld with a single method called main. Class members appear between curly braces { and } following the class name. HelloWorld has only one method, and no fields.
The main methods only parameter is an array of String objects that are the programs arguments from the command line with which it was invoked. Arrays and strings are covered later, as well as what args means for the main method.
The main method is declared void because it doesnt return a value. It is one of a few special method names in Java: The main method of a class, if declared as above, is executed when you run the class as an application. When run, main can create objects, evaluate expressions, invoke other methods, and do anything else needed to define an applications behavior.
In the example above, main contains a single statement that invokes a method on the System classs out object. Methods are invoked by supplying an object reference and a method name, separated by a dot (.). HelloWorld uses the out objects println method to print a newline-terminated string on the standard output stream.
Exercise 13.1
Enter, compile, and run HelloWorld on your system.
Exercise 13.2
Try changing parts of HelloWorld and see what errors you get.
The next example prints the Fibonacci sequencean infinite sequence whose first few terms are
1 1 2 3 5 8 13 21 34
The Fibonacci sequence starts with the terms 1 and 1, and successive terms are the sum of the previous two terms. A Fibonacci printing program is simple, but it demonstrates how to declare variables, write a simple loop, and perform basic arithmetic. Here is the Fibonacci program:
class Fibonacci { /** Print out the Fibonacci sequence for values < 50 */ public static void main (String[] args) { int lo = 1; int hi = 1; System.out.println(lo); while (hi < 50) { System.out.println(hi); hi = lo + hi; // new hi lo = hi - lo; /* new lo is (sum-old lo) i.e., the old hi */ } } }
This example declares a Fibonacci class that, like HelloWorld, has a main method. The first two lines of main declare two variables, hi and lo. Every variable must have a type that precedes its name. hi and lo are of type int, 32-bit signed integers with values in the range -232 through 232-1.
Java has built-in primitive data types to support integers, floating-point, Boolean, and character values. These primitive types hold data that Java understands directly, as opposed to object types defined by programmers. Java has no default types; the type of every variable must be defined explicitly. The primitive data types of Java are
boolean | Either true or false |
char | 16-bit Unicode 1.1 character |
byte | 8-bit integer (signed) |
short | 16-bit integer (signed) |
int | 32-bit integer (signed) |
long | 64-bit integer (signed) |
float | 32-bit floating point (IEEE 754-1985) |
double | 64-bit floating point (IEEE 754-1985) |
In the Fibonacci program, we declared hi and lo with initial values of 1. The starting values are set by initialization expressions, using the = operator, when the variables are declared. The = operator sets the variable named on the left-hand side to the value of the expression on the right-hand side. In this program, hi is the last term in the series and lo is the previous term.
Variables are undefined prior to initialization. Should you try to use variables before assigning a value, the Java compiler will refuse to compile your program until you fix the problem.
The while statement in the example provides one way of looping in Java. The expression inside the while is evaluatedif true, the body of the loop is executed and the expression tested again. The while is repeated until the expression becomes false. If it never becomes false, the program will run forever, unless something intervenes to break out of the loop, like a break statement, or an exception happening.
The expression that while tests is a Boolean expression that has the value true or false. The Boolean expression above tests whether the current high value of the sequence is less than 50. If the high value is less than 50, its value is printed and the next value calculated. If the high value equals or exceeds 50, control passes to the first line of code following the body of the while loop. That is the end of the main method in this example, so the program is finished.
Notice that the println method accepts an integer argument in the Fibonacci example above, whereas it accepted a string argument in the HelloWorld example. The println method is one of many methods that are overloaded such that they can accept arguments of different types.
Previous | Table of Contents | Next |