Java Reference
In-Depth Information
10.7
✓
✓
Describe primitive-type wrapper classes.
Check
10.8
Can each of the following statements be compiled?
Point
a.
Integer i =
new
Integer(
"23"
);
b.
Integer i =
new
Integer(
23
);
c.
Integer i = Integer.valueOf(
"23"
);
d.
Integer i = Integer.parseInt(
"23"
,
8
);
e.
Double d =
new
Double();
f.
Double d = Double.valueOf(
"23.45"
);
g.
int
i = (Integer.valueOf(
"23"
)).intValue();
h.
double
d = (Double.valueOf(
"23.4"
)).doubleValue();
i.
int
i = (Double.valueOf(
"23.4"
)).intValue();
j.
String s = (Double.valueOf(
"23.4"
)).toString();
10.9
How do you convert an integer into a string? How do you convert a numeric string
into an integer? How do you convert a double number into a string? How do you
convert a numeric string into a double value?
10.10
Show the output of the following code.
public class
Test {
public static void
main(String[] args) {
Integer x =
new
Integer(
3
);
System.out.println(x.intValue());
System.out.println(x.compareTo(
new
Integer(
4
)));
}
}
10.11
What is the output of the following code?
public class
Test {
public static void
main(String[] args) {
System.out.println(Integer.parseInt(
"10"
));
System.out.println(Integer.parseInt(
"10"
,
10
));
System.out.println(Integer.parseInt(
"10"
,
16
));
System.out.println(Integer.parseInt(
"11"
));
System.out.println(Integer.parseInt(
"11"
,
10
));
System.out.println(Integer.parseInt(
"11"
,
16
));
}
}
and Wrapper Class Types
A primitive type value can be automatically converted to an object using a wrapper
class, and vice versa, depending on the context.
Key
Point
Converting a primitive value to a wrapper object is called
boxing
. The reverse conversion is
called
unboxing
. Java allows primitive types and wrapper classes to be converted automati-
cally. The compiler will automatically box a primitive value that appears in a context requir-
ing an object, and will unbox an object that appears in a context requiring a primitive value.
This is called
autoboxing
and
autounboxing
.
boxing
unboxing
autoboxing
autounboxing
Search WWH ::
Custom Search