Chapter 5.3.2 – Wrapper Classes and Autoboxing | Introduction to Programming Using Java

Chapter 5.3.2 – Wrapper Classes and Autoboxing | Introduction to Programming Using Java

 

5.3.2 Wrapper Classes and Autoboxing

 

wrapper classes and autoboxing

 

We have already encountered the classes Double and Integer in Subsection 2.5.7. These classes contain the static methods Double.parseDouble and Integer.parseInteger that are used to convert strings to numerical values. We have also encountered the Character class in some examples, and static methods such as Character.isLetter, which can be used to test whether a given value of type char is a letter. There is a similar class for each of the other primitive types, Long, Short, Byte, Float, and Boolean.

These classes are called wrapper classes. Although they contain useful static members, they have another use as well: They are used for creating objects that represent primitive type values.

Remember that the primitive types are not classes, and values of primitive type are not objects. However, sometimes it’s useful to treat a primitive value as if it were an object. You can’t do that literally, but you can “wrap” the primitive type value in an object belonging to one of the wrapper classes.

For example, an object of type Double contains a single instance variable, of type double. The object is a wrapper for the double value. For example, you can create an object that wraps the double value 6.0221415e23 with

 

2 2 Chapter 5.3.2 - Wrapper Classes and Autoboxing | Introduction to Programming Using Java

 

The value of d contains the same information as the value of type double, but it is an object. If you want to retrieve the double value that is wrapped in the object, you can call the function d.doubleValue().

Similarly, you can wrap an int in an object of type Integer, a boolean value in an object of type Boolean, and so on. (As an example of where this would be useful, the collection classes that will be studied in Chapter 10 can only hold objects. If you want to add a primitive type value to a collection, it has to be put into a wrapper object first.)

In Java 5.0, wrapper classes have become easier to use. Java 5.0 introduced automatic conversion between a primitive type and the corresponding wrapper class. For example, if you use a value of type int in a context that requires an object of type Integer, the int will automatically be wrapped in an Integer object. For example, you can say

 

This is called autoboxing. It works in the other direction, too. For example, if d refers to an object of type Double, you can use d in a numerical expression such as 2*d. The double value inside d is automatically unboxed and multiplied by 2. Autoboxing and unboxing also apply to subroutine calls.

For example, you can pass an actual parameter of type int to a subroutine that has a formal parameter of type Integer. In fact, autoboxing and unboxing make it possible in many circumstances to ignore the difference between primitive types and objects.

The wrapper classes contain a few other things that deserve to be mentioned. Integer, for example, contains constants Integer.MINVALUE and Integer.MAXVALUE, which are equal to the largest and smallest possible values of type int, that is, to -2147483648 and 2147483647 respectively. It’s certainly easier to remember the names than the numerical values. There are similar named constants in Long, Short, and Byte.

Double and Float also have constants named MIN VALUE and MAXVALUE. MAXVALUE still gives the largest number that can be represented in the given type, but MIN VALUE represents the smallest possible positive value. For type double, Double.MINVALUE is 4.9 times 10−324. Since double values have only a finite accuracy, they can’t get arbitrarily close to zero. This is the closest they can get without actually being equal to zero.

The class Double deserves special mention, since doubles are so much more complicated than integers. The encoding of real numbers into values of type double has room for a few special values that are not real numbers at all in the mathematical sense. These values are given by named constants in class Double: Double.POSITIVEINFINITY, Double.NEGATIVEINFINITY, and Double.NaN. The infinite values can occur as the values of certain mathematical expressions.

For example, dividing a positive number by zero will give the result Double.POSITIVEINFINITY. (It’s even more complicated than this, actually, because the double type includes a value called “negative zero”, written -0.0. Dividing a positive number by negative zero gives Double.NEGATIVEINFINITY.) You also get Double.POSITIVEINFINITY whenever the mathematical value of an expression is greater than Double.MAXVALUE. For example, 1e200*1e200 is considered to be infinite.

The value Double.NaN is even more interesting. “NaN” stands for Not a Number, and it represents an undefined value such as the square root of a negative number or the result of dividing zero by zero. Because of the existence of Double.NaN, no mathematical operation on real numbers will ever throw an exception; it simply gives Double.NaN as the result.

You can test whether a value, x, of type double is infinite or undefined by calling the boolean-valued static functions Double.isInfinite(x) and Double.isNaN(x). (It’s especially important to use Double.isNaN() to test for undefined values, because Double.NaN has really weird behavior when used with relational operators such as ==. In fact, the values of x == Double.NaN and x != Double.NaN are both false, no matter what the value of x, so you really can’t use these expressions to test whether x is Double.NaN.)

Chapter 5.3.2 - Wrapper Classes and Autoboxing | Introduction to Programming Using Java

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5 – Objects and Classes | Introduction to Programming Using Java

Chapter 5.1.1 – Objects, Classes and Instances | Introduction to Programming Using Java

Chapter 5.1.2 – Fundamentals of Objects | Introduction to Programming Using Java

Chapter 5.1.3 – Getters and Setters | Introduction to Programming Using Java

Chapter 5.2 – Constructors and Object Initialization | Introduction to Programming Using Java

Chapter 5.2.2 – Constructors | Introduction to Programming Using Java

1 thought on “Chapter 5.3.2 – Wrapper Classes and Autoboxing | Introduction to Programming Using Java”

Leave a Comment