Primitive vs Wrapper Types
Integer, Double, Boolean and friends.
Primitive vs Wrapper Types is a free Java Academy lesson on CoddyKit — lesson 1 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.
Two Worlds of Values
Java has primitive types like int, double, and boolean that hold raw values directly. It also has wrapper classes like Integer, Double, and Boolean that wrap those values inside objects.
Primitives are fast and lightweight. Wrappers are full objects that can live in collections and can be null.
The Eight Primitives
Java defines exactly eight primitive types:
byte,short,int,longfor integersfloat,doublefor decimalscharfor a single characterbooleanfor true/false
Each has a matching wrapper class in the java.lang package.
Matching Wrapper Names
Most wrapper names just capitalize the primitive, but two differ:
intbecomes Integercharbecomes Characterbooleanbecomes Boolean,doublebecomes Double, and so on
Let us print a few wrappers.
public class Main {
public static void main(String[] args) {
Integer count = 42;
Double price = 9.99;
Boolean active = true;
Character grade = 'A';
System.out.println(count);
System.out.println(price);
System.out.println(active);
System.out.println(grade);
}
}Wrappers Are Objects
A primitive holds a value. A wrapper is an object reference, so it can point to nothing at all with null.
This makes wrappers useful when a value might be missing, like a database column that allows NULL.
public class Main {
public static void main(String[] args) {
Integer missing = null;
int present = 5;
System.out.println(missing);
System.out.println(present);
}
}Useful Constants
Wrapper classes expose handy constants. Integer.MAX_VALUE and Integer.MIN_VALUE give the largest and smallest int values.
This is far easier than memorizing the numbers yourself.
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
}
}Helper Methods
Wrapper classes are also toolboxes of static helper methods. For example, Integer.toBinaryString shows a number in base 2, and Integer.max returns the larger of two values.
public class Main {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(10));
System.out.println(Integer.max(7, 3));
System.out.println(Integer.toHexString(255));
}
}Why Collections Need Wrappers
Java generics work only with objects, not primitives. You cannot write List<int>.
Instead you use List<Integer>. The wrapper makes the primitive value fit into the object world that collections require.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
System.out.println(numbers);
}
}Comparing Wrapper Values
To compare the numeric value inside two wrappers, use .equals() or .compareTo(), not the math operators on the objects themselves.
compareTo returns a negative number, zero, or a positive number.
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 200;
System.out.println(a.equals(b));
System.out.println(a.compareTo(b));
}
}Memory and Performance
Primitives use less memory and run faster because there is no object overhead. A single int is just 4 bytes of raw data.
A wrapper object carries extra header information. For tight loops over millions of values, prefer primitives.
Defaults Differ
An uninitialized primitive field defaults to a zero-like value: int is 0, boolean is false.
An uninitialized wrapper field defaults to null. This difference matters when you later try to use the value.
public class Main {
static int primitiveField;
static Integer wrapperField;
public static void main(String[] args) {
System.out.println(primitiveField);
System.out.println(wrapperField);
}
}Choosing Between Them
A simple rule: use primitives by default for performance and clarity. Switch to a wrapper when you need a collection element, a generic type, or the ability to represent a missing value with null.
Quick Check
Test your understanding of primitives and wrappers.
Recap
You learned the difference between primitives and wrappers:
- Eight primitives map to eight wrapper classes such as
IntegerandDouble - Wrappers are objects, can be
null, and fit into collections - Primitives are faster and lighter, so use them by default
- Wrapper classes also provide useful constants and helper methods
Frequently asked questions
Is the “Primitive vs Wrapper Types” lesson free?
Yes — the full text of “Primitive vs Wrapper Types” 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 “Primitive vs Wrapper Types”?
Integer, Double, Boolean and friends. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Primitive vs Wrapper Types” 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