Dove's LibraryDove's Library

4.7 - Wrapper Classes

The Integer and Double classes live in java.lang, and both are immutable, once you create one, it can’t be changed.
Wrapper classes exist because some parts of Java only work with objects. The big one: an ArrayList (4.8) can only hold object references, so you can’t make an ArrayList<int>. You use ArrayList<Integer> instead. Since everything in java.lang is available automatically, Integer and Double never need an import statement.
Autoboxing is when Java automatically converts a primitive (like int) into its wrapper object (Integer) for you, no explicit cast needed. This happens when you pass a primitive to something expecting the wrapper type, or assign a primitive directly to a wrapper variable.
Integer boxedNum = 5; // autoboxing, int automatically becomes Integer
Autoboxing also happens when you pass an int to a method that expects an Integer, like adding an int to an ArrayList<Integer>:
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(7); // add expects an Integer, so 7 is autoboxed
Unboxing is the reverse, Java automatically converts a wrapper object back to its primitive type when needed.
Integer boxedNum = 5; int plainNum = boxedNum; // unboxing, Integer automatically becomes int
Unboxing also happens when you pass an Integer to a method that expects an int, or use a wrapper object in math:
Integer negative = -12; int distance = Math.abs(negative); // Math.abs expects an int, so negative is unboxed: 12 Double price = 4.99; double total = price * 2; // price is unboxed to do the math: 9.98
Immutable means the value inside an Integer or Double object can never change. When it looks like you’re changing one, Java is really unboxing it, doing the math, and boxing the result into a different object:
Integer count = 5; count = count + 1; // count now refers to a different Integer object holding 6
Two handy static methods for converting strings to numbers:
static int parseInt(String s) // "42" becomes 42 static double parseDouble(String s) // "3.14" becomes 3.14
int age = Integer.parseInt("16"); double price = Double.parseDouble("9.99");
These come up most when numbers arrive as text, like a line read from a file and split into pieces (4.6). The String has to look like a valid number of that type, or you get a NumberFormatException (Integer.parseInt("3.5") fails, for example). Watch the difference parsing makes:
String s = "16"; System.out.println(s + 1); // 161, string concatenation System.out.println(Integer.parseInt(s) + 1); // 17, real addition

AP® is a registered trademark of the College Board, which is not affiliated with Dove's Library and was not involved in the production of, and does not endorse, this website.

Providing free, high-quality AP notes to help students succeed, earn college credit, and reach their educational goals, ensuring everyone has access to the resources they need.

© 2026 Standard Learning Group, Inc., which operates Dove's Library. All rights reserved.