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

Thinking in Java - 4th Edition

DisciplinaProgramação Orientada A Objetos5.222 materiais79.387 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\u2019ll 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\u2019re interested in is println( ), which in effect means \u201cPrint what I\u2019m giving you 
out to the console and end with a newline.\u201d 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\u2019re 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\u2019t 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\u2019t 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\u2019t 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\u2019s 
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 \u201cproperties\u201d 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\u2014in 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\u2019s 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\u2019ve set up your computer\u2019s 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\u2019t 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\u2019ll 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 \u201cAnt\u201d commands for automatically building the files for that 
6 IBM\u2019s \u201cjikes\u201d compiler is a common alternative, as it is significantly faster than Sun\u2019s javac (although if you\u2019re building 
groups of files using Ant, there\u2019s 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 \u2018ant\u2019 at the command 
prompt to compile and run the programs in each chapter. If you haven\u2019t 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\u2019ll often see: 
/* This is a comment 
 * that continues 
 * across lines 
Remember, however, that everything inside the /* and */ is ignored, so there\u2019s 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\u2019s easy. You don\u2019t need to hunt on the keyboard to find / and then * 
(instead, you just press the same key twice), and you don\u2019t 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