Java is an object-oriented language, but it also uses primitive types (like int and double) for performance. To bridge this gap, Java provides wrapper classes (`Integer` and `Double`) that wrap primitive values inside an object wrapper.
Key Concepts to Remember Before You Start:
- Autoboxing: The automatic conversion that Java makes from a primitive type to its corresponding wrapper object (e.g., assigning an
intto anIntegervariable). - Unboxing: The automatic conversion from a wrapper object back to a primitive type (e.g., passing an
Integerobject into a math operation expecting anint). - The Wrapper Comparison Trap: Just like Strings, wrapper objects must be compared using
.equals()rather than==, because==checks memory addresses.
Test your knowledge with these 10 practice questions. Click “View Answer & Explanation” to see how autoboxing behaves behind the scenes.
Topic 2.9 Practice
int in Java?int is capitalized as Integer. Similarly, the wrapper class for double is Double, and boolean is Boolean.int) into an object of its wrapper class (like an Integer) without requiring explicit constructor calls.int x = num;
System.out.println(x);
int x = num;)?Integer) back into its primitive equivalent (int) by the Java compiler.Integer val2 = 100;
System.out.println(val1 == val2);
val1 == val2 evaluates to true. (Though best practice on the AP exam is always to use .equals() for objects).Integer b = 1000;
System.out.println(a == b);
== operator compares memory references, which are different, returning false. This highlights why comparing wrapper objects with == is dangerous.int result = n;
System.out.println(result);
null Integer object into a primitive int (int result = n;), it tries to retrieve a primitive value from a non-existent object. This instantly throws a NullPointerException.double sum = d + 2.0;
System.out.println(sum);
+ expects numeric primitive values, Java automatically unboxes the Double object d into a primitive double (5.5). It then performs standard mathematical addition with 2.0, resulting in 7.5.Integer.MAX_VALUE and Integer.MIN_VALUE are frequently tested on the AP exam for algorithm boundaries.Integer y = 5;
System.out.println(x.equals(y));
.equals() method on wrapper classes compares the actual numerical value stored inside the object rather than their memory addresses. Since both objects represent the value 5, x.equals(y) returns true.ArrayList data structure (Unit 7)?ArrayList are designed to hold objects, not raw primitives. Wrapper classes allow you to store integers and doubles inside collections seamlessly thanks to automatic autoboxing.Unit 2.9 Practice Complete!
The Golden Rule of Wrapper Classes
Just like with Strings, never use == to compare two Integer or Double objects unless you are intentionally checking memory addresses. Always use .equals() to compare their numerical values safely.
Ready for the Final Challenge?
You have successfully built and reviewed all four sub-topics of Unit 2: String Methods, the Math class, Object References, and Wrapper Classes. You are now fully prepared for the comprehensive exam test.
Test your complete mastery by taking the Unit 2 Cumulative Mock Exam!