1.2 A First Program
We begin our study of Java with a program that prints a message greeting the world.
Example 1
class Greet
{
// This program prints a simple message
public static void main (String[] args)
{
System.out.println("Hello, world");
}
}
|
|
Let us look in detail at the components of the program in Example 1.
- class
All Java programs are contained in a class. The start of a class
is indicated by the reserved word class. Java contains about fifty
reserved words, all of which are listed later in this chapter.
- Greet
This is the name that identifies the class. The choice of a name is,
within limits, up to the programmer but, customarily, classes have
names that start with an upper case letter. We chose the name Greet
because the program greets the world but we could have called it
FirstProgram if we had chosen to do so. In fact, as far as the
computer is concerned, we could have called the class Athena or
Saskatoon but it is better, for the benefit of your human readers,
to use a meaningful name.
- { }
The beginning and end of any section of a Java program are indicated
by brace brackets, { and }. The beginning and end of the class are
indicated by the { in the second line and the } in the last line of the
program.
- // This program ...
The two adjacent slashes indicate a comment in a Java program.
Anything on a line after // is ignored by the compiler. Although
they do not affect the way that the program works, comments can
be very useful to any person trying to understand a program. A
comment can also be indicated in a Java program by using /* to
start the comment and */ to end it. Using this style, we could have
written our comment as
/* This program prints a simple message */
Usually, comments that appear entirely on one line are written using
the // style while those that occupy multiple lines are written using
the /* ... */ style.
- public static void main (String[] args)
Java programs consist of one or more methods, (sometimes called
functions or procedures in other programming languages). If the program
is run directly by the Java interpreter, then exactly one of its
methods must be called main. (Such programs are sometimes called application programs. As we will see later when
we study applets, Java has other types of programs that do not have a main method.) Our program has only one method so
it must be called main. This line is the header of the main method.
The other words and symbols in the header will be explained later.
For now, just use them, exactly as shown.
- { }
The inner pair of brace brackets defines the beginning and end of the
definition of the main method, just as the outer pair of brace brackets
defines the limits of the definition of the class.
- System.out.println("Hello, world");
This statement calls on a method called println to send the string
of characters Hello world contained in double quotes to some output
device (probably your computer's screen). Whatever the output
device is, we can think of it as a page of paper. The println method,
given a string of characters, writes the string on the page and then
moves on to the next line of the page, ready to print the next item, if
there is one. As we suggested in discussing the header of the method,
the details of the use of println will be made clear later. For now,
just use it exactly as shown, with an upper case S on System, dots between
System, out, and println, and including both the parentheses
and the terminating semi-colon.
You should notice in Example 1 the way in which the parts of the
program have been indented. Everything within the brace brackets that
enclose the body of the class is indented two spaces. Similarly, everything
within the brace brackets that enclose the body of the main method is
indented two more spaces past the header of the method.
Indentation is not necessary for the computer but it can improve the
readability for a human. Since your programs should be clear as well as
correct, you should always indent them. Your instructor might suggest an
indentation style that is slightly different from the one that we use. The
important thing is to use a style that is both clear and consistent.
Example 2
As far as a computer is concerned, the program of the previous example is
exactly equivalent to either of the following:
class Greet{public static void main(String[]
args){System.out.println("Hello, world");}}
or
class Greet {
public
static
void
main( String [
] args )
{
System.out.println
( "Hello, world"
) ;
}
}
|
|
Notice the ways in which blanks are used in Example 2.
- At least one blank must be used to separate two adjacent words.
Thus, classGreet or voidmain would be incorrect.
- If a symbol appears between two words, it can be used as a separator
so that a blank is no longer required. For example, main(String and
main ( String are equivalent.
- Extra blanks, even entire blank lines, do not usually change the meaning
of the surrounding symbols. Blanks can even be inserted before
or after dots so that System . out . println would be correct.
(It is, however, considered bad style to have blanks around dots; you
should avoid it.) Finally, extra blanks inside a string (inside double
quotes) will not be ignored so that the output produced by printing
the strings "Hello , world" and "Hello, world" would be different.
A number of variations can be made to our basic printing technique.
- To print a blank line, we can simply write
System.out.println();
If we want to print a string that is longer than we have room for on
one line of our program, we cannot simply continue the string onto
a second line. If we were to do so, the compiler would get upset and
refuse to compile our program. Instead, to print a very long string,
we can write something like the following:
System.out.println("This will print a verrrrrry long"
+ " string on one line by joining"
+ " these strings into one string");
The process of joining strings is called concatenation. The plus sign
used with strings acts as the concatenation operator in Java.
- If we do not wish to jump to a new line after printing a string, we
can use the method print instead of println as illustrated in the
next example.
Example 3
The following program illustrates some uses of print and println.
class PrintDemo
{
public static void main (String[] args)
{
System.out.println("Never put off till tomorrow");
System.out.println();
System.out.print("what you can do");
System.out.println(" the day");
System.out.println();
System.out.println("after" + " " + "tomorrow.");
}
}
This will produce the following output.
Never put off till tomorrow
what you can do the day
after tomorrow.
|
|
|
Since the beginning and end of a string to be written by print or
println are marked by double quotes, how do we print a string that contains
double quotes? Java's solution is to use a backslash character, \, immediately
in front of a double quote to signal the computer that it should
print the character ".
Example 4
The statement
System.out.println("Angela said, "
+ "\"That's ridiculous, Bram.\"");
would print
Angela said, "That's ridiculous, Bram."
|
|
|
The technique used to print a double quote can be used in a number
of other ways. For example, to print a backslash, you should write \\.
To jump to a new line, you should write \n. (The n indicates a new line.)
These pairs are called escape sequences. A complete list of escape sequences
can be found in Appendix C.
Example 5
The statement
System.out.println("A backslash: \\\na double qute: \"");
would produce the output
A backslash: \
a double quote: "
|
|
|
Exercises 1.2
- What does this program print?
class Advice
{
public static void main (String[] args)
{
System.out.print("If at first ");
System.out.println("you don't succeed" + ",");
System.out.print("failure may be ");
System.out.println("your style");
}
}
- Rewrite the following program using the indentation style shown in
the text.
class Quote{public static void main(String[]args){
System.out.print("The unexamined life");
System.out.println(" is not worth living.");}}
Write a single Java statement that would print the following, exactly
as shown.
A slash is "/"
while
a backslash is "\"
|
- The following program contains a number of errors. Rewrite the
program with the errors corrected.
class BadForm
public void main (string() args);
{
System.Out.Println('What's wrong with this?')
}
- Write a Java program to print your name in giant letters as shown
in the following example.
BBBB EEEEE A TTTTT RRRR III X X
B B E A A T R R I X X
B B E A A T R R I X X
BBBB EEE AAAAA T RRRR I X
B B E A A T R R I X X
B B E A A T R R I X X
BBBB EEEEE A A T R R III X X
- Write Java programs to print each design. You might find it helpful
to use squared paper in planning the appearance of your output.
PARALLELOG APEZO D
A R R I I I
R A TRAPEZOID A A
ALLELOGRAM M M
O O
N N
D
- Write a complete Java program to print your name and address as
they would appear on the envelope of a letter written to you at your
home.
|
|