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

Thinking in Java - 4th Edition

DisciplinaProgramação Orientada A Objetos4.949 materiais75.284 seguidores
Pré-visualização50 páginas
a \u2018//:\u2019 as a special marker for the 
comment line containing the source file name. That line contains the path information to the 
file (object indicates this chapter) followed by the file name. The last line also finishes with a 
comment, and this one (\u2018///:~\u2019) indicates the end of the source code listing, which allows it to 
be automatically updated into the text of this book after being checked with a compiler and 
Everything Is an Object 59 
60 Thinking in Java Bruce Eckel 
The /* Output: tag indicates the beginning of the output that will be generated by this file. 
In this form, it can be automatically tested to verify its accuracy. In this case, the (55% 
match) indicates to the testing system that the output will be fairly different from one run to 
the next so it should only expect a 55 percent correlation with the output shown here. Most 
examples in this book that produce output will contain the output in this commented form, 
so you can see the output and know that it is correct. 
Coding style 
The style described in the Code Conventions for the Java Programming Language7 is to 
capitalize the first letter of a class name. If the class name consists of several words, they are 
run together (that is, you don\u2019t use underscores to separate the names), and the first letter of 
each embedded word is capitalized, such as: 
class AllTheColorsOfTheRainbow { // ... 
This is sometimes called \u201ccamel-casing.\u201d For almost everything else\u2014methods, fields 
(member variables), and object reference names\u2014the accepted style is just as it is for classes 
except that the first letter of the identifier is lowercase. For example: 
class AllTheColorsOfTheRainbow { 
 int anIntegerRepresentingColors; 
 void changeTheHueOfTheColor(int newHue) { 
 // ... 
 // ... 
The user must also type all these long names, so be merciful. 
The Java code you will see in the Sun libraries also follows the placement of open-and-close 
curly braces that you see used in this book. 
The goal of this chapter is just enough Java to understand how to write a simple program. 
You\u2019ve also gotten an overview of the language and some of its basic ideas. However, the 
examples so far have all been of the form \u201cDo this, then do that, then do something else.\u201d The 
next two chapters will introduce the basic operators used in Java programming, and then 
show you how to control the flow of your program. 
Normally, exercises will be distributed throughout the chapters, but in this chapter you were 
learning how to write basic programs so all the exercises were delayed until the end. 
The number in parentheses after each exercise number is an indicator of how difficult the 
exercise is, in a ranking from 1-10. 
Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, 
available for sale from 
7 To preserve space in this book and seminar presentations, not all of 
these guidelines could be followed, but you\u2019ll see that the style I use here matches the Java standard as much as possible. 
Exercise 1: (2) Create a class containing an int and a char that are not initialized, and 
print their values to verify that Java performs default initialization. 
Exercise 2: (1) Following the example in this chapter, create a \u201chello, 
world\u201d program that simply displays that statement. You need only a single method in your 
class (the \u201cmain\u201d one that gets executed when the program starts). Remember to make it 
static and to include the argument list, even though you don\u2019t use the argument list. Compile 
the program with javac and run it using java. If you are using a different development 
environment than the JDK, learn how to compile and run programs in that environment. 
Exercise 3: (1) Find the code fragments involving ATypeName and turn them into a 
program that compiles and runs. 
Exercise 4: (1) Turn the DataOnly code fragments into a program that compiles and 
Exercise 5: (1) Modify the previous exercise so that the values of the data in DataOnly 
are assigned to and printed in main( ). 
Exercise 6: (2) Write a program that includes and calls the storage( ) method defined 
as a code fragment in this chapter. 
Exercise 7: (1) Turn the Incrementable code fragments into a working program. 
Exercise 8: (3) Write a program that demonstrates that, no matter how many objects 
you create of a particular class, there is only one instance of a particular static field in that 
Exercise 9: (2) Write a program that demonstrates that autoboxing works for all the 
primitive types and their wrappers. 
Exercise 10: (2) Write a program that prints three arguments taken from the command 
line. To do this, you\u2019ll need to index into the command-line array of Strings. 
Exercise 11: (1) Turn the AllTheColorsOfTheRainbow example into a program that 
compiles and runs. 
Exercise 12: (2) Find the code for the second version of, which is the 
simple comment documentation example. Execute Javadoc on the file and view the results 
with your Web browser. 
Exercise 13: (1) Run,, and through Javadoc. Verify the resulting documentation with your 
Web browser. 
Exercise 14: (1) Add an HTML list of items to the documentation in the previous 
Exercise 15: (1) Take the program in Exercise 2 and add comment documentation to it. 
Extract this comment documentation into an HTML file using Javadoc and view it with 
your Web browser. 
Everything Is an Object 61 
62 Thinking in Java Bruce Eckel 
Exercise 16: (1) In the Initialization & Cleanup chapter, locate the 
example and add Javadoc documentation. Extract this comment documentation into an 
HTML file using Javadoc and view it with your Web browser. 
At the lowest level, data in Java is manipulated using operators. 
Because Java was inherited from C++, most of these operators will be familiar to C and C++ 
programmers. Java has also added some improvements and simplifications. 
If you\u2019re familiar with C or C++ syntax, you can skim through this chapter and the next, 
looking for places where Java is different from those languages. However, if you find yourself 
floundering a bit in these two chapters, make sure you go through the multimedia seminar 
Thinking in C, freely downloadable from It contains audio lectures, 
slides, exercises, and solutions specifically designed to bring you up to speed with the 
fundamentals necessary to learn Java. 
Simpler print statements 
In the previous chapter, you were introduced to the Java print statement: 
System.out.println("Rather a lot to type"); 
You may observe that this is not only a lot to type (and thus many redundant tendon hits), 
but also rather noisy to read. Most languages before and after Java have taken a much 
simpler approach to such a commonly used statement. 
The Access Control chapter introduces the concept of the static import that was added to 
Java SE5, and creates a tiny library to simplify writing print statements. However, you don\u2019t 
need to know those details in order to begin using that library. We can rewrite the program 
from the last chapter using this new library: 
//: operators/ 
import java.util.*; 
import static net.mindview.util.Print.*; 
public class HelloDate { 
 public static void main(String[] args) { 
 print("Hello, it\u2019s: "); 
 print(new Date()); 
} /* Output: (55% match) 
Hello, it\u2019s: 
Wed Oct 05 14:39:05 MDT 2005 
The results are much cleaner. Notice the insertion of the static keyword in the second 
import statement. 
In order to use this library, you must download this book\u2019s code package from or one of its