Java Reference
In-Depth Information
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 blue-
prints and instructions to build gadgetsobjects are the gadgets the fact-
ory 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 val-
ues 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
byte-
codes,
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 Microsys-
temsyou put the source for
HelloWorld
into a file named
HelloWorld.java
.
To compile it you type the command
javac HelloWorld.java