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

Thinking in Java - 4th Edition

Disciplina:Programação Orientada a Objetos1.257 materiais32.015 seguidores
Pré-visualização50 páginas
a ‘//:’ 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 (‘///:~’) 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
executed.

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’t use underscores to separate the names), and the first letter of
each embedded word is capitalized, such as:

class AllTheColorsOfTheRainbow { // ...

This is sometimes called “camel-casing.” For almost everything else—methods, fields
(member variables), and object reference names—the 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.

Summary
The goal of this chapter is just enough Java to understand how to write a simple program.
You’ve also gotten an overview of the language and some of its basic ideas. However, the
examples so far have all been of the form “Do this, then do that, then do something else.” 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.

Exercises
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 www.MindView.net.

                                                            
7 http://java.sun.com/docs/codeconv/index.html. To preserve space in this book and seminar presentations, not all of
these guidelines could be followed, but you’ll 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 HelloDate.java example in this chapter, create a “hello,
world” program that simply displays that statement. You need only a single method in your
class (the “main” one that gets executed when the program starts). Remember to make it
static and to include the argument list, even though you don’t 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
runs.

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
class.

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’ll 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 HelloDate.java, which is the
simple comment documentation example. Execute Javadoc on the file and view the results
with your Web browser.

Exercise 13: (1) Run Documentation1.java, Documentation2.java, and
Documentation3.java 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.

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 Overloading.java
example and add Javadoc documentation. Extract this comment documentation into an
HTML file using Javadoc and view it with your Web browser.

 

Operators
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’re 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 www.MindView.net. 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’t
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/HelloDate.java
import java.util.*;
import static net.mindview.util.Print.*;
public class HelloDate {
 public static void main(String[] args) {
 print("Hello, it’s: ");
 print(new Date());
 }
} /* Output: (55% match)
Hello, it’s:
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’s code package from
www.MindView.net or one of its