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.

396 Chapter 10 Thinking in Objects<br />

✓Point✓ Check<br />

10.14 Describe primitive-type wrapper classes.<br />

10.15 Can each of the following statements be <strong>com</strong>piled?<br />

a. Integer i = new Integer("23");<br />

b. Integer i = new Integer(23);<br />

c. Integer i = Integer.valueOf("23");<br />

d. Integer i = Integer.parseInt("23", 8);<br />

e. Double d = new Double();<br />

f. Double d = Double.valueOf("23.45");<br />

g. int i = (Integer.valueOf("23")).intValue();<br />

h. double d = (Double.valueOf("23.4")).doubleValue();<br />

i. int i = (Double.valueOf("23.4")).intValue();<br />

j. String s = (Double.valueOf("23.4")).<strong>to</strong>String();<br />

10.16 How do you convert an integer in<strong>to</strong> a string? How do you convert a numeric string<br />

in<strong>to</strong> an integer? How do you convert a double number in<strong>to</strong> a string? How do you convert<br />

a numeric string in<strong>to</strong> a double value?<br />

10.17 Show the output of the following code.<br />

public class Test {<br />

public static void main(String[] args) {<br />

Integer x = new Integer(3);<br />

System.out.println(x.intValue());<br />

System.out.println(x.<strong>com</strong>pareTo(new Integer(4)));<br />

}<br />

}<br />

10.18 What is the output of the following code?<br />

public class Test {<br />

public static void main(String[] args) {<br />

System.out.println(Integer.parseInt("10"));<br />

System.out.println(Integer.parseInt("10", 10));<br />

System.out.println(Integer.parseInt("10", 16));<br />

System.out.println(Integer.parseInt("11"));<br />

System.out.println(Integer.parseInt("11", 10));<br />

System.out.println(Integer.parseInt("11", 16));<br />

}<br />

}<br />

boxing<br />

unboxing<br />

au<strong>to</strong>boxing<br />

au<strong>to</strong>unboxing<br />

Key<br />

Point<br />

10.13 Au<strong>to</strong>matic Conversion between Primitive<br />

Types and Wrapper Class Types<br />

A primitive type value can be au<strong>to</strong>matically converted <strong>to</strong> an object using a wrapper<br />

class, and vice versa, depending on the context.<br />

Converting a primitive value <strong>to</strong> a wrapper object is called boxing. The reverse conversion is<br />

called unboxing. <strong>Java</strong> allows primitive types and wrapper classes <strong>to</strong> be converted au<strong>to</strong>matically.<br />

The <strong>com</strong>piler will au<strong>to</strong>matically box a primitive value that appears in a context<br />

requiring an object, and will unbox an object that appears in a context requiring a primitive<br />

value. This is called au<strong>to</strong>boxing and au<strong>to</strong>unboxing.

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

Saved successfully!

Ooh no, something went wrong!