2.8 Wrapper Classes: Integers and Doubles
As you saw with strings, using Java packages can make your program easier to implement. In this section, you are going to explore two other members of the java.lang
package, the Integer
and Double
classes.
A wrapper class in Java is a class that contains or “wraps” primitive data types as an object. You will find wrapper classes for all of our primitive data types. One thing to note about the wrapper classes is that they have similar (if not the same) names as the primitive data types, but they all begin with capital letters.
Primitive Type | Corresponding Wrapper Class |
---|---|
boolean |
Boolean |
char |
Character |
int |
Integer |
double |
Double |
The wrapper classes in Java actually serve two purposes. First, as their name implies, they can take primitive data types and convert them into an object. Later in the course, you will see some examples of when you need to use an object representation of primitive data.
Second, wrapper classes provide static methods that allow you to perform some basic number operations, such as converting data from a string to a number. This means you can do some basic operations without creating an instance of the object. This section will take a closer look at some of these methods within the Integer
and Double
classes.
Creating wrapper objects, such as Integer
and Double
objects can be done in a similar way to other objects. The constructor takes a single value and converts it into the object version of that value.
Fortunately, you don’t need to explicitly create Integer
and Double
objects every time you need to use them. Java provides a process called autoboxing. Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int
to an Integer
and a double
to a Double
.
The Java compiler applies autoboxing when a primitive value is passed as a parameter to a method that expects an object or if a primitive value is assigned to a variable of the corresponding wrapper class.
Similarly, Java has a process for unboxing. Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer
to an int
and a Double
to a double
. The Java compiler applies unboxing when a wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type or a wrapper class object is assigned to a variable of the corresponding primitive type.
In addition to creating objects, the wrapper classes also provide several useful methods, some of which are static. As a reminder, a static method is a method that can be used without creating an instance of the object. You use the class name followed by a dot and the method name to call static methods.
Here are some useful methods for the Integer
and Double
classes.
Wrapper Class Methods | Use |
---|---|
Integer.MIN_VALUE |
Returns the minimum possible int or Integer value |
Integer.MAX_VALUE |
Returns the maximum possible int or Integer value |
varInt.intValue() |
Converts Integer value into an int value |
varDbl.doubleValue() |
Converts Double value into a double value |
Here is an example of how you can use these methods.
Here is the documentation for Integer
:
https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/Integer.html
Here is the documentation for Double
:
https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/Double.html
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Java automatically converts between objects and primitives in the process of autoboxing and unboxing.
What happens when an
Integer
is unboxed?
The Extremes
class represents the range of Integer
values.
Fill in the Extremes
methods. In the constructor, set min
and max
to the minimum and maximum value of Integer
. Use Integer.MIN_VALUE
and Integer.MAX_VALUE
. Also, fill in the minDiff
and maxDiff
methods to return the difference between the user’s guess and the actual max/min values of Integer
.
Main
In main
, use the comments as a guide to ask the user to guess the maximum and minimum values. Compute the difference between the guess and the values, then print it out.
Be aware that if you choose a number too big or too small, it will cause an error since the number cannot be stored.
The results in main
should look something like this: