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.

Call<strong>in</strong>g the new operator on a class type causes three events to occur:<br />

• A new object is dynamically allocated <strong>in</strong> memory, <strong>and</strong> all <strong>in</strong>stance<br />

variables are <strong>in</strong>itialized to st<strong>and</strong>ard default values. The default values are null<br />

for object variables <strong>and</strong> 0 for all base types except boolean variables (which are<br />

false by default).<br />

• The constructor for the new object is called with the parameters specified.<br />

The constructor fills <strong>in</strong> mean<strong>in</strong>gful values for the <strong>in</strong>stance variables <strong>and</strong> performs<br />

any additional computations that must be done to create this object.<br />

• After the constructor returns, the new operator returns a reference (that is,<br />

a memory address) to the newly created object. If the expression is <strong>in</strong> the form of<br />

an assignment statement, then this address is stored <strong>in</strong> the object variable, so the<br />

object variable refers to this newly created object.<br />

Number Objects<br />

We sometimes want to store numbers as objects, but base type numbers are not<br />

themselves objects, as we have noted. To get around this obstacle, <strong>Java</strong> def<strong>in</strong>es a<br />

wrapper class for each numeric base type. We call these classes number classes.<br />

In Table 1.2, we show the numeric base types <strong>and</strong> their correspond<strong>in</strong>g number<br />

class, along with examples of how number objects are created <strong>and</strong> accessed. S<strong>in</strong>ce<br />

<strong>Java</strong> 5.0, a creation operation is performed automatically any time we pass a base<br />

number to a method expect<strong>in</strong>g a correspond<strong>in</strong>g object. Likewise, the<br />

25

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

Saved successfully!

Ooh no, something went wrong!