Thinking in Java - 4th Edition
1079 pág.

Thinking in Java - 4th Edition

Disciplina:Programação Orientada a Objetos1.257 materiais32.010 seguidores
Pré-visualização50 páginas
can just use it. What you can do with this out object is
determined by its type: PrintStream. Conveniently, PrintStream is shown in the
description as a hyperlink, so if you click on that, you’ll see a list of all the methods you can
call for PrintStream. There are quite a few, and these will be covered later in the book. For
now all we’re interested in is println( ), which in effect means “Print what I’m giving you
out to the console and end with a newline.” Thus, in any Java program you write you can
write something like this:

System.out.println("A String of things");

whenever you want to display information to the console.

The name of the class is the same as the name of the file. When you’re creating a standalone
program such as this one, one of the classes in the file must have the same name as the file.
(The compiler complains if you don’t do this.) That class must contain a method called
main( ) with this signature and return type:

public static void main(String[] args) {

The public keyword means that the method is available to the outside world (described in
detail in the Access Control chapter). The argument to main( ) is an array of String objects.
The args won’t be used in this program, but the Java compiler insists that they be there
because they hold the arguments from the command line.

The line that prints the date is quite interesting:

System.out.println(new Date());

The argument is a Date object that is being created just to send its value (which is
automatically converted to a String) to println( ). As soon as this statement is finished,
that Date is unnecessary, and the garbage collector can come along and get it anytime. We
don’t need to worry about cleaning it up.

5 The Java compiler and documentation from Sun tend to change regularly, and the best place to get them is directly from
Sun. By downloading it yourself, you will get the most recent version.

54 Thinking in Java Bruce Eckel

When you look at the JDK documentation from, you will see that
System has many other methods that allow you to produce interesting effects (one of Java’s
most powerful assets is its large set of standard libraries). For example:

//: object/
public class ShowProperties {
 public static void main(String[] args) {
} ///:~

The first line in main( ) displays all of the “properties” from the system where you are
running the program, so it gives you environment information. The list( ) method sends the
results to its argument, System.out. You will see later in the book that you can send the
results elsewhere, to a file, for example. You can also ask for a specific property—in this case,
the user name and java.library.path. (The unusual comments at the beginning and end
will be explained a little later.)

Compiling and running

To compile and run this program, and all the other programs in this book, you must first
have a Java programming environment. There are a number of third-party development
environments, but in this book I will assume that you are using the Java Developer’s Kit
(JDK) from Sun, which is free. If you are using another development system,6 you will need
to look in the documentation for that system to determine how to compile and run programs.

Get on the Internet and go to There you will find information and links
that will lead you through the process of downloading and installing the JDK for your
particular platform.

Once the JDK is installed, and you’ve set up your computer’s path information so that it will
find javac and java, download and unpack the source code for this book (you can find it at This will create a subdirectory for each chapter in this book. Move to
the subdirectory named objects and type:


This command should produce no response. If you get any kind of an error message, it means
you haven’t installed the JDK properly and you need to investigate those problems.

On the other hand, if you just get your command prompt back, you can type:

java HelloDate

and you’ll get the message and the date as output.

This is the process you can use to compile and run each of the programs in this book.
However, you will see that the source code for this book also has a file called build.xml in
each chapter, and this contains “Ant” commands for automatically building the files for that

6 IBM’s “jikes” compiler is a common alternative, as it is significantly faster than Sun’s javac (although if you’re building
groups of files using Ant, there’s not too much of a difference). There are also open-source projects to create Java
compilers, runtime environments, and libraries.

chapter. Buildfiles and Ant (including where to download it) are described more fully in the
supplement you will find at, but once you have Ant
installed (from you can just type ‘ant’ at the command
prompt to compile and run the programs in each chapter. If you haven’t installed Ant yet, you
can just type the javac and java commands by hand.

Comments and embedded

There are two types of comments in Java. The first is the traditional C-style comment that
was inherited by C++. These comments begin with a /* and continue, possibly across many
lines, until a */. Note that many programmers will begin each line of a continued comment
with a *, so you’ll often see:

/* This is a comment
 * that continues
 * across lines

Remember, however, that everything inside the /* and */ is ignored, so there’s no difference
in saying:

/* This is a comment that
continues across lines */

The second form of comment comes from C++. It is the single-line comment, which starts
with a // and continues until the end of the line. This type of comment is convenient and
commonly used because it’s easy. You don’t need to hunt on the keyboard to find / and then *
(instead, you just press the same key twice), and you don’t need to close the comment. So you
will often see:

// This is a one-line comment

Comment documentation

Possibly the biggest problem with documenting code has been maintaining that
documentation. If the documentation and the code are separate, it becomes tedious to
change the documentation every time you change the code. The solution seems simple: Link
the code to the documentation. The easiest way to do this is to put everything in the same file.
To complete the picture, however, you need a special comment syntax to mark the
documentation and a tool to extract those comments and put them in a useful form. This is
what Java has done.

The tool to extract the comments is called Javadoc, and it is part of the JDK installation. It
uses some of the technology from the Java compiler to look for special comment tags that you
put in your programs. It not only extracts the information marked by these tags, but it also
pulls out the class name or method name that adjoins the comment. This way you can get
away with the minimal amount of work to generate decent program documentation.

The output of Javadoc is an HTML file that you can view with your Web browser. Thus,
Javadoc allows you to create and maintain a single source file and automatically generate
useful documentation. Because of Javadoc, you have a straightforward standard for creating
documentation, so you can expect or even demand documentation with all Java libraries.

Everything Is an Object 55 

In addition, you can write your own Javadoc handlers, called doclets, if you want to perform
special operations on the information processed by