Java Reference
In-Depth Information
Example 1−1: Hello.java (continued)
System.out.println("Hello World!"); // Say hello!
}
// This marks the end of main()
}
// Marks the end of the class
The first line of this program is the package declaration. It specifies the name of
the package of which this program is part. The program's name (as we'll see in the
second line) is Hello . The package name is com.davidflanagan.examples.basics .
We can combine these two names to produce a fully qualified name, com.david-
flanagan.examples.basics.Hello . Using packages provides a unique namespace
for every Java program. By placing this Hello program in a package, I've ensured
that no naming conflict will arise if someone else defines a program that is also
named Hello . To ensure that the package name I've chosen is unique, I've fol-
lowed the convention of reversing the components of my Internet domain name
to produce a package name prefix. * Each chapter of this topic has its own pack-
age that begins with the prefix com.davidflanagan.examples . In this case, since
this is the basics chapter,
the package name is com.davidflanagan.exam-
ples.basics .
The value of “Hello World” is that it is a template that you can expand on in your
later experiments with Java. The second and third lines lines of Example 1-1 are a
required part of the template. Every program—every piece of Java code, really—
you write is a class. The second line of the example says that we're writing a class
named Hello . It also says the class is public , which means it can be used by any-
one.
Every standalone Java program requires a main() method. This is where the Java
interpreter begins running a Java program. The third line of the example declares
this main() method. It says that the method is public , that it has no return value
(i.e., its return value is void ), and that it is passed an array of strings as its argu-
ment. The name of the array is args . The line also says that main() is a static
method. (In this chapter, we work exclusively with static methods. In Chapter 2,
when we start working with objects, you'll learn what a static method is, and
you'll see that non- static methods are actually the norm.)
In any case, you might as well go ahead and memorize this line:
public static void main(String[] args)
Every standalone Java program you ever write contains a line that looks exactly
like this one. (Actually, you can name the array of strings anything you want, but
it is usually called args .)
The fifth and sixth lines of Example 1-1 simply mark the end of the main()
method and of the Hello class. Like most modern programming languages, Java is
a block-structured language. This means that such things as classes and methods
have bodies that comprise a “block” of code. In Java, the beginning of a block is
marked by a { , and the end is marked by a matching } . Method blocks are always
defined within class blocks, and as we'll see in later examples, methods blocks can
contain such things as if statements and for loops that form subblocks within the
* My web site is http://www.davidflanagan.com , so my package name begins com.davidflanagan .
Search WWH ::




Custom Search