Results! Why, man, I have gotten a lot of results. I know several thousand things that won't
work.
Thomas Edison
Calvin and Hobbes quote (page 39) reprinted by permission of Universal Press Syndicate.
"Mail Myself to You," (page 183) words and music by Woody Guthrie. trO 1962 (renewed), 1963 (renewed)
by Ludlow Music, Inc. New York, NY. Used by permission.
Chapter 1. A Quick Tour
See Europe! Ten Countries in Seventeen Days!
Sign in a travel agent's window
This chapter is a whirlwind tour of the Java™ programming language that gets you started writing code
quickly. We briefly cover the main points of the language, without slowing you down with full-blown detail.
Subsequent chapters contain detailed discussions of specific features.
1.1. Getting Started
In the Java programming language, 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 the factory makes.
A class contains members, the primary kinds being fields and methods. Fields are data variables 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. Statements define the behavior of
the classes: they can assign values to fields and other variables, evaluate arithmetic expressions, invoke
methods, and control the flow of execution.
Long tradition holds that the first sample program for any language should print "Hello, world":
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
Use your favorite text editor to type this program source code into a file. Then run the compiler to compile the
source of this program into bytecodes, the "machine language" for the Java virtual machine (more on this later
in this chapter). Details of editing and compiling source vary from system to systemconsult your system
manuals for specific information. On the system we use most oftenthe Java™ 2 Platform, Standard Edition
(J2SE™) Development Kit (JDK) provided free of charge by Sun Microsystemsyou put the source for
HelloWorld into a file named HelloWorld.java. To compile it you type the command
javac HelloWorld.java