Java Reference
In-Depth Information
System
is the name of a standard class that contains objects that encapsulate the standard I/O
devices for your system - the keyboard for command line input and command line output to
the display. It is contained in the package
java.lang
so it is always accessible just by using
the simple class name,
System
.
❑
The object
out
represents the standard output stream - the command line on your display
screen, and is a data member of the class
System
. The member,
out
, is a special kind of
member of the
System
class. Like the method
main()
in our
OurFirstProgram
class, it is
static
. This means that
out
exists even though there are no objects of type
System
(more
on this in forthcoming chapters). Using the class name,
System
, separated from the member
name
out
by a period -
System.out
, references the
out
member.
❑
The bit at the rightmost end of the statement,
println("Krakatoa
,
EAST
of
Java??")
,
calls the
println()
method that belongs to the object
out,
and that outputs the text string
that appears between the parentheses to your display. This demonstrates one way in which
you can call a class method - by using the object name followed by the method name, with a
period separating them. The stuff between the parentheses following the name of a method is
information that is passed to the method when it is executed. As we said, for
println()
it is
the text we want to output to the command line.
❑
For completeness, the keywords
public
,
static
,
and
void
, that appear in the method
definition are explained briefly in the annotations to the program code, but you need not be
concerned if these still seem a bit obscure at this point. We will be coming back to them in much
more detail later on.
You can compile this program using the JDK compiler with the command,
javac -source 1.4 OurFirstProgram.java
Or with the
-classpath
option specified:
javac -source 1.4 -classpath . OurFirstProgram.java
If it didn't compile, there's something wrong somewhere. Here's a checklist of possible sources
of the problem:
You forgot to include the path to the
jdk1.4\bin
directory in your
PATH
, or maybe you
did not specify the path correctly. This will result in your operating system not being able to
find the
javac
compiler that is in that directory.
❑
You made an error typing in the program code. Remember Java is case sensitive so
OurfirstProgram
is not the same as
OurFirstProgram
, and of course, there must be no
spaces in the class name. If the compiler discovers an error it will usually identify the line
number in the code where the error was found. In general, watch out for confusing zero,
0
,
with a small letter,
o
, or the digit one,
1
, with the small letter
l
. All characters such as periods,
commas, and semicolons in the code are essential, and must be in the right place. Parentheses,
(), curly braces, {}, and square brackets, [], always come in matching pairs and are not
interchangeable.
❑
The source file name must match the class name exactly. The slightest difference will result in
an error. It must have the extension
.java
.
❑