23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Dot Operator<br />

Every object reference variable must refer to some object, unless it is null, <strong>in</strong><br />

which case it po<strong>in</strong>ts to noth<strong>in</strong>g. Us<strong>in</strong>g the remote control analogy, a null reference<br />

is a remote control holder that is empty. Initially, unless we assign an object<br />

variable to po<strong>in</strong>t to someth<strong>in</strong>g, it is null.<br />

There can, <strong>in</strong> fact, be many references to the same object, <strong>and</strong> each reference to a<br />

specific object can be used to call methods on that object. Such a situation would<br />

correspond to our hav<strong>in</strong>g many remote controls that all work on the same device.<br />

Any of the remotes can be used to make a change to the device (like chang<strong>in</strong>g a<br />

channel on a television). Note that if one remote control is used to change the<br />

device, then the (s<strong>in</strong>gle) object po<strong>in</strong>ted to by all the remotes changes. Likewise, if<br />

we use one object reference variable to change the state of the object, then its state<br />

changes for all the references to it. This behavior comes from the fact that there<br />

are many references, but they all po<strong>in</strong>t to the same object.<br />

One of the primary uses of an object reference variable is to access the members<br />

of the class for this object, an <strong>in</strong>stance of its class. That is, an object reference<br />

variable is useful for access<strong>in</strong>g the methods <strong>and</strong> <strong>in</strong>stance variables associated with<br />

an object. This access is performed with the dot (".") operator. We call a method<br />

associated with an object by us<strong>in</strong>g the reference variable name, follow<strong>in</strong>g that by<br />

the dot operator <strong>and</strong> then the method name <strong>and</strong> its parameters.<br />

This calls the method with the specified name for the object referred to by this<br />

object reference. It can optionally be passed multiple parameters. If there are<br />

several methods with this same name def<strong>in</strong>ed for this object, then the <strong>Java</strong><br />

runtime system uses the one that matches the number of parameters <strong>and</strong> most<br />

closely matches their respective types. A method's name comb<strong>in</strong>ed with the<br />

number <strong>and</strong> types of its parameters is called a method's signature, for it takes all<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!