19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

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

394 Chapter 10 Thinking in Objects<br />

java.lang.Integer<br />

java.lang.Double<br />

-value: int<br />

+MAX_VALUE: int<br />

+MIN_VALUE: int<br />

+Integer(value: int)<br />

+Integer(s: String)<br />

+byteValue(): byte<br />

+shortValue(): short<br />

+intValue(): int<br />

+longVlaue(): long<br />

+floatValue(): float<br />

+doubleValue(): double<br />

+<strong>com</strong>pareTo(o: Integer): int<br />

+<strong>to</strong>String(): String<br />

+valueOf(s: String): Integer<br />

+valueOf(s: String, radix: int): Integer<br />

+parseInt(s: String): int<br />

+parseInt(s: String, radix: int): int<br />

-value: double<br />

+MAX_VALUE: double<br />

+MIN_VALUE: double<br />

+Double(value: double)<br />

+Double(s: String)<br />

+byteValue(): byte<br />

+shortValue(): short<br />

+intValue(): int<br />

+longVlaue(): long<br />

+floatValue(): float<br />

+doubleValue(): double<br />

+<strong>com</strong>pareTo(o: Double): int<br />

+<strong>to</strong>String(): String<br />

+valueOf(s: String): Double<br />

+valueOf(s: String, radix: int): Double<br />

+parseDouble(s: String): double<br />

+parseDouble(s: String, radix: int): double<br />

FIGURE 10.14<br />

data types.<br />

The wrapper classes provide construc<strong>to</strong>rs, constants, and conversion methods for manipulating various<br />

no no-arg construc<strong>to</strong>r<br />

immutable<br />

constants<br />

The wrapper classes do not have no-arg construc<strong>to</strong>rs. The instances of all wrapper classes<br />

are immutable; this means that, once the objects are created, their internal values cannot be<br />

changed.<br />

Each numeric wrapper class has the constants MAX_VALUE and MIN_VALUE. MAX_VALUE<br />

represents the maximum value of the corresponding primitive data type. For Byte, Short,<br />

Integer, and Long, MIN_VALUE represents the minimum byte, short, int, and long values.<br />

For Float and Double, MIN_VALUE represents the minimum positive float and<br />

double values. The following statements display the maximum integer (2,147,483,647),<br />

the minimum positive float (1.4E–45), and the maximum double floating-point number<br />

(1.79769313486231570e+308d).<br />

System.out.println("The maximum integer is " + Integer.MAX_VALUE);<br />

System.out.println("The minimum positive float is " +<br />

Float.MIN_VALUE);<br />

System.out.println(<br />

"The maximum double-precision floating-point number is " +<br />

Double.MAX_VALUE);<br />

conversion methods<br />

Each numeric wrapper class contains the methods doubleValue(), floatValue(),<br />

intValue(), longValue(), and shortValue() for returning a double, float, int,<br />

long, or short value for the wrapper object. For example,<br />

new Double("12.4").intValue() returns 12;<br />

new Integer("12").doubleValue() returns 12.0;<br />

<strong>com</strong>pareTo method<br />

Recall that the String class contains the <strong>com</strong>pareTo method for <strong>com</strong>paring two strings.<br />

The numeric wrapper classes contain the <strong>com</strong>pareTo method for <strong>com</strong>paring two numbers<br />

and returns 1, 0, or -1, if this number is greater than, equal <strong>to</strong>, or less than the other number.<br />

For example,

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

Saved successfully!

Ooh no, something went wrong!