Autoboxing and Unboxing
Automatic conversion rules.
Autoboxing and Unboxing is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Automatic Conversion
Autoboxing is Java automatically converting a primitive into its wrapper object. Unboxing is the reverse, turning a wrapper back into a primitive.
This happens silently so you can mix the two worlds without manual conversion calls.
Boxing in Action
Assigning an int to an Integer variable triggers autoboxing. The compiler inserts a call to Integer.valueOf for you.
public class Main {
public static void main(String[] args) {
int primitive = 42;
Integer boxed = primitive;
System.out.println(boxed);
}
}Unboxing in Action
Assigning an Integer to an int triggers unboxing. The compiler inserts a call to intValue behind the scenes.
public class Main {
public static void main(String[] args) {
Integer boxed = 99;
int primitive = boxed;
System.out.println(primitive);
}
}Boxing in Collections
The most common place you see autoboxing is when adding primitives to a collection. You write list.add(5) and Java boxes the 5 into an Integer automatically.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);
int first = nums.get(0);
System.out.println(first);
}
}Arithmetic Unboxes
When you use math operators on wrapper objects, Java unboxes them to primitives first, does the math, then can rebox the result.
Below, two Integer values are added by unboxing both.
public class Main {
public static void main(String[] args) {
Integer a = 10;
Integer b = 32;
int sum = a + b;
System.out.println(sum);
}
}Mixing Primitives and Wrappers
You can freely mix a primitive and a wrapper in an expression. Java unboxes the wrapper so both sides are primitives during the calculation.
public class Main {
public static void main(String[] args) {
Integer wrapped = 7;
int raw = 3;
int product = wrapped * raw;
System.out.println(product);
}
}Boxing in Method Calls
Autoboxing also applies to method arguments. A method that expects an Object or a wrapper can receive a primitive, and Java boxes it on the way in.
public class Main {
static void show(Object value) {
System.out.println("Got: " + value);
}
public static void main(String[] args) {
show(123);
show(true);
}
}The Hidden Cost
Autoboxing is convenient but not free. Each boxing creates an object, and tight loops can create millions of short-lived objects.
For performance-critical numeric loops, work with primitives directly rather than wrappers.
public class Main {
public static void main(String[] args) {
long total = 0;
for (int i = 0; i < 5; i++) {
total += i;
}
System.out.println(total);
}
}valueOf Versus new
Autoboxing uses Integer.valueOf, which caches small values between -128 and 127. Calling new Integer(...) always makes a fresh object and is now deprecated.
Prefer letting autoboxing do its job.
public class Main {
public static void main(String[] args) {
Integer cached = Integer.valueOf(50);
System.out.println(cached);
}
}Ternary and Boxing
A subtle rule: in a ternary expression, if one branch is a primitive and the other is a wrapper, Java may unbox both to a common primitive type.
This is mostly safe, but be aware it can trigger conversions you did not expect.
public class Main {
public static void main(String[] args) {
boolean flag = true;
Integer result = flag ? 1 : 2;
System.out.println(result);
}
}When It Helps
Autoboxing exists to make code readable when mixing primitives with generics and collections. Used wisely it removes clutter. Used carelessly in hot loops it hurts performance.
Quick Check
Check your grasp of autoboxing rules.
Recap
You learned how autoboxing and unboxing work:
- Autoboxing turns a primitive into a wrapper using
valueOf - Unboxing turns a wrapper back into a primitive using methods like
intValue - It happens in assignments, collections, arithmetic, and method calls
- It is convenient but creates objects, so avoid it in hot loops
Frequently asked questions
Is the “Autoboxing and Unboxing” lesson free?
Yes — the full text of “Autoboxing and Unboxing” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Autoboxing and Unboxing”?
Automatic conversion rules. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Autoboxing and Unboxing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Primitive vs Wrapper Types
- Autoboxing and Unboxing
- Parsing and Valueof
- Autoboxing Pitfalls