19.09.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

10.12 Processing Primitive Data Type Values as Objects 393<br />

public class SomeThing {<br />

private int t1;<br />

private static int t2;<br />

}<br />

public SomeThing(int t1, int t2) {<br />

...<br />

}<br />

(a)<br />

public class SomeThing {<br />

private int t1;<br />

private static int t2;<br />

}<br />

public SomeThing(int t1) {<br />

...<br />

}<br />

public static void setT2(int t2) {<br />

SomeThing.t2 = t2;<br />

}<br />

(b)<br />

Instance and static are integral parts of object-oriented programming. A data field or<br />

method is either instance or static. Do not mistakenly overlook static data fields or methods.<br />

It is a <strong>com</strong>mon design error <strong>to</strong> define an instance method that should have been static. For<br />

example, the fac<strong>to</strong>rial(int n) method for <strong>com</strong>puting the fac<strong>to</strong>rial of n should be<br />

defined static, because it is independent of any specific instance.<br />

A construc<strong>to</strong>r is always instance, because it is used <strong>to</strong> create a specific instance. A static<br />

variable or method can be invoked from an instance method, but an instance variable or<br />

method cannot be invoked from a static method.<br />

10.13 Describe class design guidelines.<br />

10.12 Processing Primitive Data Type Values as Objects<br />

A primitive type value is not an object, but it can be wrapped in an object using a<br />

wrapper class in the <strong>Java</strong> API.<br />

Owing <strong>to</strong> performance considerations, primitive data type values are not objects in <strong>Java</strong>.<br />

Because of the overhead of processing objects, the language’s performance would be<br />

adversely affected if primitive data type values were treated as objects. However, many <strong>Java</strong><br />

methods require the use of objects as arguments. <strong>Java</strong> offers a convenient way <strong>to</strong> incorporate,<br />

or wrap, a primitive data type in<strong>to</strong> an object (e.g., wrapping int in<strong>to</strong> the Integer class, and<br />

wrapping double in<strong>to</strong> the Double class). Recall that a char value can be wrapped in<strong>to</strong> a<br />

Character object in Section 9.5. By using a wrapper class, you can process primitive data<br />

type values as objects. <strong>Java</strong> provides Boolean, Character, Double, Float, Byte, Short,<br />

Integer, and Long wrapper classes in the java.lang package for primitive data types. The<br />

Boolean class wraps a Boolean value true or false. This section uses Integer and Double<br />

as examples <strong>to</strong> introduce the numeric wrapper classes.<br />

Note<br />

Most wrapper class names for a primitive type are the same as the primitive data type<br />

name with the first letter capitalized. The exceptions are Integer and Character.<br />

Numeric wrapper classes are very similar <strong>to</strong> each other. Each contains the methods<br />

doubleValue(), floatValue(), intValue(), longValue(), shortValue(), and<br />

byteValue(). These methods “convert” objects in<strong>to</strong> primitive type values. The key features<br />

of Integer and Double are shown in Figure 10.14.<br />

You can construct a wrapper object either from a primitive data type value or from a string<br />

representing the numeric value—for example, new Double(5.0), new Double("5.0"),<br />

new Integer(5), and new Integer("5").<br />

<strong>com</strong>mon design error<br />

✓Point✓ Check<br />

Key<br />

Point<br />

why wrapper class?<br />

naming convention<br />

construc<strong>to</strong>rs

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

Saved successfully!

Ooh no, something went wrong!