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

Thinking in Java - 4th Edition

Disciplina:Programação Orientada a Objetos1.262 materiais32.145 seguidores
Pré-visualização50 páginas
This means that you can accurately
represent integral values of any size without losing any information during operations.

BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate
monetary calculations, for example.

Consult the JDK documentation for details about the constructors and methods you can call
for these two classes.

Arrays in Java

Virtually all programming languages support some kind of arrays. Using arrays in C and C++
is perilous because those arrays are only blocks of memory. If a program accesses the array
outside of its memory block or uses the memory before initialization (common programming
errors), there will be unpredictable results.

One of the primary goals of Java is safety, so many of the problems that plague programmers
in C and C++ are not repeated in Java. A Java array is guaranteed to be initialized and cannot

44 Thinking in Java Bruce Eckel

be accessed outside of its range. The range checking comes at the price of having a small
amount of memory overhead on each array as well as verifying the index at run time, but the
assumption is that the safety and increased productivity are worth the expense (and Java can
sometimes optimize these operations).

When you create an array of objects, you are really creating an array of references, and each
of those references is automatically initialized to a special value with its own keyword: null.
When Java sees null, it recognizes that the reference in question isn’t pointing to an object.
You must assign an object to each reference before you use it, and if you try to use a reference
that’s still null, the problem will be reported at run time. Thus, typical array errors are
prevented in Java.

You can also create an array of primitives. Again, the compiler guarantees initialization
because it zeroes the memory for that array.

Arrays will be covered in detail in later chapters.

You never need to
 destroy an object

In most programming languages, the concept of the lifetime of a variable occupies a
significant portion of the programming effort. How long does the variable last? If you are
supposed to destroy it, when should you? Confusion over variable lifetimes can lead to a lot
of bugs, and this section shows how Java greatly simplifies the issue by doing all the cleanup
work for you.

Scoping

Most procedural languages have the concept of scope. This determines both the visibility and
lifetime of the names defined within that scope. In C, C++, and Java, scope is determined by
the placement of curly braces {}. So for example:

{
 int x = 12;
 // Only x available
{
 int q = 96;
 // Both x & q available
}
 // Only x available
 // q is "out of scope"
}

A variable defined within a scope is available only to the end of that scope.

Any text after a ‘//’ to the end of a line is a comment.

Indentation makes Java code easier to read. Since Java is a free-form language, the extra
spaces, tabs, and carriage returns do not affect the resulting program.

You cannot do the following, even though it is legal in C and C++:

{
 int x = 12;
 {

Everything Is an Object 45 

 int x = 96; // Illegal
 }
}

The compiler will announce that the variable x has already been defined. Thus the C and C++
ability to “hide” a variable in a larger scope is not allowed, because the Java designers
thought that it led to confusing programs.

Scope of objects

Java objects do not have the same lifetimes as primitives. When you create a Java object
using new, it hangs around past the end of the scope. Thus if you use:

{
 String s = new String("a string");
} // End of scope

the reference s vanishes at the end of the scope. However, the String object that s was
pointing to is still occupying memory. In this bit of code, there is no way to access the object
after the end of the scope, because the only reference to it is out of scope. In later chapters
you’ll see how the reference to the object can be passed around and duplicated during the
course of a program.

It turns out that because objects created with new stay around for as long as you want them,
a whole slew of C++ programming problems simply vanish in Java. In C++ you must not only
make sure that the objects stay around for as long as you need them, you must also destroy
the objects when you’re done with them.

That brings up an interesting question. If Java leaves the objects lying around, what keeps
them from filling up memory and halting your program? This is exactly the kind of problem
that would occur in C++. This is where a bit of magic happens. Java has a garbage collector,
which looks at all the objects that were created with new and figures out which ones are not
being referenced anymore. Then it releases the memory for those objects, so the memory can
be used for new objects. This means that you never need to worry about reclaiming memory
yourself. You simply create objects, and when you no longer need them, they will go away by
themselves. This eliminates a certain class of programming problem: the so-called “memory
leak,” in which a programmer forgets to release memory.

Creating new data types: class
If everything is an object, what determines how a particular class of object looks and
behaves? Put another way, what establishes the type of an object? You might expect there to
be a keyword called “type,” and that certainly would have made sense. Historically, however,
most objectoriented languages have used the keyword class to mean “I’m about to tell you
what a new type of object looks like.” The class keyword (which is so common that it will not
usually be boldfaced throughout this book) is followed by the name of the new type. For
example:

class ATypeName { /* Class body goes here */ }

This introduces a new type, although the class body consists only of a comment (the stars and
slashes and what is inside, which will be discussed later in this chapter), so there is not too
much that you can do with it. However, you can create an object of this type using new:

ATypeName a = new ATypeName();

46 Thinking in Java Bruce Eckel

But you cannot tell it to do much of anything (that is, you cannot send it any interesting
messages) until you define some methods for it.

Fields and methods

When you define a class (and all you do in Java is define classes, make objects of those
classes, and send messages to those objects), you can put two types of elements in your class:
fields (sometimes called data members), and methods (sometimes called member functions).
A field is an object of any type that you can talk to via its reference, or a primitive type. If it is
a reference to an object, you must initialize that reference to connect it to an actual object
(using new, as seen earlier).

Each object keeps its own storage for its fields; ordinary fields are not shared among objects.
Here is an example of a class with some fields:

class DataOnly {
 int i;
 double d;
 boolean b;
}

This class doesn’t do anything except hold data. But you can create an object like this:

DataOnly data = new DataOnly();

You can assign values to the fields, but you must first know how to refer to a member of an
object. This is accomplished by stating the name of the object reference, followed by a period
(dot), followed by the name of the member inside the object:

objectReference.member

For example:

data.i = 47;
data.d = 1.1;
data.b = false;

It is also possible that your object might contain other objects that contain data you’d like to
modify. For this, you just keep “connecting the dots.” For example:

myPlane.leftTank.capacity = 100;

The DataOnly class cannot do much of anything except hold data, because it has no
methods. To understand how those work, you must first understand arguments and return
values, which will be described shortly.

Default values