System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə24/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   20   21   22   23   24   25   26   27   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

class­var = new class­name(arg­list
Here, class­var is a variable of the class type being created. The class­name is the name
of the class that is being instantiated. The class name followed by a parenthesized
argument list (which can be empty) specifies the constructor for the class. If a class
does not define its own constructor, new will use the default constructor supplied by
Java. Thus, new can be used to create an object of any class type. The new operator
returns a reference to the newly created object, which (in this case) is assigned to class­
var.
Since memory is finite, it is possible that new will not be able to allocate memory for an
object because insufficient memory exists. If this happens, a run­time exception will
occur. (You will learn about exceptions in 
Chapter 9
.) For the sample programs in this
book, you won’t need to worry about running out of memory, but you will need to
consider this possibility in real­world programs that you write.
GARBAGE COLLECTION
As you have seen, objects are dynamically allocated from a pool of free memory by
using the new operator. As explained, memory is not infinite, and the free memory can
be exhausted. Thus, it is possible for new to fail because there is insufficient free
memory to create the desired object. For this reason, a key component of any dynamic
allocation scheme is the recovery of free memory from unused objects, making that
memory available for subsequent reallocation. In some programming languages, the
release of previously allocated memory is handled manually. However, Java uses a
different, more trouble­free approach: garbage collection.
Java’s garbage collection system reclaims objects automatically—occurring
transparently, behind the scenes, without any programmer intervention. It works like


this: When no references to an object exist, that object is assumed to be no longer
needed, and the memory occupied by the object is released. This recycled memory can
then be used for a subsequent allocation.
Ask the Expert
Q
: Why don’t I need to use new for variables of the primitive types,
such as int or float?
A
: Java’s primitive types are not implemented as objects. Rather, because of
efficiency concerns, they are implemented as “normal” variables. A variable of a
primitive type actually contains the value that you have given it. As explained,
object variables are references to the object. This layer of indirection (and other
object features) adds overhead to an object that is avoided by a primitive type.
Garbage collection occurs only sporadically during the execution of your program. It
will not occur simply because one or more objects exist that are no longer used. For
efficiency, the garbage collector will usually run only when two conditions are met:
there are objects to recycle, and there is a reason to recycle them. Remember, garbage
collection takes time, so the Java run­time system does it only when it is appropriate.
Thus, you can’t know precisely when garbage collection will take place.
THE THIS KEYWORD
Before concluding this chapter, it is necessary to introduce this. When a method is
called, it is automatically passed an implicit argument that is a reference to the invoking
object (that is, the object on which the method is called). This reference is called this.
To understand this, first consider a program that creates a class called Pwr that
computes the result of a number raised to some integer power:


As you know, within a method, the other members of a class can be accessed directly,
without any object or class qualification. Thus, inside get_pwr( ), the statement
means that the copy of val associated with the invoking object will be returned.
However, the same statement can also be written like this:


Here, this refers to the object on which get_pwr( ) was called. Thus, this.val refers
to that object’s copy of val. For example, if get_pwr( ) had been invoked on x, then
this in the preceding statement would have been referring to x. Writing the statement
without using this is really just shorthand.
Here is the entire Pwr class written using the this reference:
Actually, no Java programmer would write Pwr as just shown because nothing is
gained, and the standard form is easier. However, this has some important uses. For
example, the Java syntax permits the name of a parameter or a local variable to be the
same as the name of an instance variable. When this happens, the local name hides the
instance variable. You can gain access to the hidden instance variable by referring to it
through this. For example, the following is a syntactically valid way to write the Pwr( )
constructor.
In this version, the names of the parameters are the same as the names of the instance
variables, thus hiding them. However, this is used to “uncover” the instance variables.



Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   20   21   22   23   24   25   26   27   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə