Java Reference
In-Depth Information
Self-Test Exercises
17. Which of the following are legal?
Integer n = new Integer(42);
int m = 42;
n = m;
m = n;
If any are illegal, explain how to write a valid Java statement that does what the
illegal statement is trying to do.
18. In the following, is the value of the variable price after the assignment statement
an object of the class Double or a value of the primitive type double ?
Double price = 1.99;
19. In the following, is the value of the variable count after the assignment statement
an object of the class Integer or a value of the primitive type int ?
int count = new Integer(12);
Static Methods in Wrapper Classes
The material on wrapper classes that we have seen thus far explains why they are called
wrapper classes. However, possibly more importantly, the wrapper classes contain a
number of useful constants and static methods. So, wrapper classes have two distinct
personalities: one is their ability to produce class objects corresponding to values of
primitive types, and the other is as a repository of useful constants and methods. It was
not necessary to combine these two personalities into one kind of class. Java could
have had two sets of classes, one for each personality, but the designers of the Java
libraries chose to have only one set of classes for both personalities.
You can use the associated wrapper class to find the value of the largest and small-
est values of any of the primitive number types. For example, the largest and smallest
values of type int are
largest and
smallest
values
Integer.MAX_VALUE and Integer.MIN_VALUE
The largest and smallest values of type double are
Double.MAX_VALUE and Double.MIN_VALUE
Wrapper classes have static methods that can be used to convert back and forth
between string representations of numbers and the corresponding number of type int ,
double , long , or float . For example, the static method parseDouble of the wrapper
class Double converts a string to a value of type double . So, the code
parse-
Double
Double.parseDouble("199.98")
Search WWH ::




Custom Search