Welcome to Wrapper Classes!
In your journey through Java so why, you have mostly used primitive types like \(int\), \(double\), and \(boolean\). These are great because they are fast and efficient. However, as we move into Unit 4: Data Sets and Arrays, you will discover that some Java tools—specifically the \(ArrayList\)—only work with objects, not primitives.
Don't worry if that sounds confusing! Wrapper classes are here to bridge the gap. Think of them as a "fancy gift box" for your primitive values. They allow us to wrap a simple number inside an object so it can go anywhere an object is required.
1. What are Wrapper Classes?
A wrapper class is a class that "wraps" a primitive data type into an object. For the AP Exam, you only need to know two specific wrapper classes:
- \(Integer\): The object version of the primitive \(int\).
- \(Double\): The object version of the primitive \(double\).
Important Note: Objects of these wrapper classes are immutable. This means once an \(Integer\) or \(Double\) object is created, its value cannot be changed. If you want a different value, you have to create a brand-new object.
2. The Integer Class
The \(Integer\) class provides several useful tools for working with whole numbers. You should be familiar with these specific features found in the Java Quick Reference:
Minimum and Maximum Values
Sometimes you need to know the range of an \(int\). Java provides two constants for this:
- \(Integer.MIN_VALUE\): The smallest possible value an \(int\) can hold (approximately \(-2\) billion).
- \(Integer.MAX_VALUE\): The largest possible value an \(int\) can hold (approximately \(+2\) billion).
Pro Tip: These are often used when looking for the "minimum" or "maximum" value in an array. For example, if you are looking for the smallest number, you might start your "minSoFar" variable at \(Integer.MAX_VALUE\).
The \(parseInt\) Method
If you have a \(String\) that contains a number (like \("42"\)) and you need to turn it into an actual math-ready \(int\), you use this static method:
\(int \text{ myNum} = Integer.parseInt("42");\)
3. The Double Class
Just like the \(Integer\) class, the \(Double\) class helps us manage decimal numbers as objects.
The \(parseDouble\) Method
This works exactly like \(parseInt\), but for decimals. It converts a \(String\) into a primitive \(double\):
\(double \text{ myPrice} = Double.parseDouble("19.99");\)
4. Autoboxing and Unboxing
You might be wondering: "Do I have to manually wrap and unwrap my numbers every time?" The answer is no! Java does most of the work for you through two processes called autoboxing and unboxing.
Autoboxing
Autoboxing is the automatic conversion the Java compiler makes between the primitive type and its corresponding object wrapper class.
Example: If you assign an \(int\) to an \(Integer\) object, Java "boxes" it for you.
\(Integer \text{ wrappedInt} = 10; // \text{ This is Autoboxing}\)
Unboxing
Unboxing is the reverse. It is the automatic conversion of a wrapper class object back into a primitive value.
Example: If you assign an \(Integer\) object to a primitive \(int\), Java "unboxes" it.
\(int \text{ primitiveInt} = \text{wrappedInt}; // \text{ This is Unboxing}\)
Key Takeaway: Because of autoboxing and unboxing, you can use \(Integer\) objects and \(int\) primitives almost interchangeably in your code. Java handles the "packaging" behind the scenes!
5. Why Do We Need This? (The Connection to ArrayList)
As you will learn in Topic 4.8: ArrayList Methods, an \(ArrayList\) can only store object references. It cannot store primitives directly.
If you try to write \(ArrayList
6. Summary and Quick Review
Quick Review Box:
- Wrapper classes allow primitives to be treated as objects.
- \(Integer\) wraps \(int\); \(Double\) wraps \(double\).
- \(Integer.MIN_VALUE\) and \(Integer.MAX_VALUE\) represent the limits of an \(int\).
- \(parseInt\) and \(parseDouble\) turn \(String\)s into numbers.
- Autoboxing: \(int \rightarrow Integer\)
- Unboxing: \(Integer \rightarrow int\)
- Immutability: Wrapper objects cannot be changed once created.
Common Mistake to Avoid: Don't forget that while \(int\) is lowercase (it's a primitive), \(Integer\) and \(Double\) start with Uppercase letters because they are Classes!
Up Next: Now that you know how to wrap your data, you are ready to explore Topic 4.8: ArrayList Methods to see these wrapper classes in action!