Java Notes
Learn Java wrapper classes — Integer, Double, Boolean and others, why they exist, autoboxing and unboxing, parsing strings to numbers, and comparing wrapper objects.
Java has two kinds of data — primitives (int, double, boolean, etc.) and objects. Primitives are fast but they are not objects: they have no methods, and they cannot be stored in collections that require objects.
Wrapper classes solve this. Each primitive type has a corresponding wrapper class that wraps it in an object.
| Primitive | Wrapper Class |
|---|---|
int | Integer |
double | Double |
boolean | Boolean |
char | Character |
long | Long |
float | Float |
byte | Byte |
short | Short |
Why Wrapper Classes Exist
Java collections like ArrayList cannot hold primitives directly — they need objects. Wrapper classes let you store int values as Integer objects.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Autoboxing — Primitive to Wrapper Automatically
Java 5+ added autoboxing — the compiler automatically converts a primitive to its wrapper class when needed.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
Integer b = 42;— Java sees anintgoing into anIntegervariable and insertsInteger.valueOf(42)for you.- This is purely a compile-time convenience — at bytecode level, it still creates an
Integerobject.
Unboxing — Wrapper to Primitive Automatically
The reverse: an Integer object is automatically converted to int when needed.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java handles boxing and unboxing transparently. You write arithmetic as if they were primitives; Java adds the conversions.
Parsing Strings to Numbers
One of the most common uses of wrapper classes: converting a String (typed by a user or read from a file) into a number.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
Integer.parseInt("25")— converts the String"25"toint25.Double.parseDouble("99.99")— converts String todouble.Boolean.parseBoolean("true")— returnstruefor"true"(case-insensitive),falsefor anything else.Integer.toString(age)— convertsintback to a String.
What happens with invalid input:
int bad = Integer.parseInt("abc"); // throws NumberFormatExceptionAlways wrap parsing in try-catch when the input comes from a user.
Useful Wrapper Class Methods
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Comparing Wrapper Objects — == vs equals()
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
Integer a = 127; Integer b = 127;— Java caches Integer values from -128 to 127. Soaandbpoint to the same cached object.a == bis true.Integer x = 200; Integer y = 200;— 200 is outside the cache range. Two separate objects are created.x == yis false (different objects).x.equals(y)is true (same value).
Rule: Always use equals() to compare wrapper objects by value. Never ==. The Integer cache is an implementation detail you should not rely on.
Common Mistake 1: Unboxing null
Integer i = null;
int x = i; // NullPointerException — cannot unbox nullA null wrapper cannot be unboxed to a primitive. Always check for null before unboxing.
if (i != null) {
int x = i;
}Common Mistake 2: Using == to Compare Integer Objects
Integer a = 200;
Integer b = 200;
if (a == b) { // false — compares references, not values
System.out.println("Equal");
}
if (a.equals(b)) { // true — compares values
System.out.println("Equal");
}Common Mistake 3: Expecting Alphabetical Parsing to Work
Integer.parseInt("one"); // NumberFormatException
Integer.parseInt("1,000"); // NumberFormatException — comma is not a digitparseInt only accepts digit characters. Strip commas and other formatting before parsing.
Practice Task
Task: Write a program that takes an array of Strings: {"45", "bad", "78", "12", "oops", "99"}. Loop through each string. If it can be parsed as an integer, add it to the total. If not, skip it. Print the total at the end.
Hint: Use a try-catch around Integer.parseInt(). If it throws NumberFormatException, skip that string.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: "bad" and "oops" trigger the catch block. The valid numbers are summed. Try adding "3.5" to the array — parseInt fails on it too (use parseDouble for decimals).
Continue the lesson path