← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 1 of 6

Java Wrapper Classes

Wrapper classes put simple values like numbers into objects, like putting a toy in a gift box so it can go where only boxes are allowed. Java wraps and unwraps them automatically.
Syntax
java
Integer object = Integer.valueOf(primitiveValue);
int primitive = object.intValue();
Integer.parseInt(string);

What are Wrapper Classes

Wrapper classes like Integer, Double, and Boolean provide object equivalents of Java's primitive types, letting primitives be used anywhere an Object is required — such as inside generic collections.

Example: What are Wrapper Classes

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<Integer> numbers = new ArrayList<>(); // Integer wraps int
		numbers.add(5);
		System.out.println(numbers.get(0));
	}
}

Understanding Autoboxing

Autoboxing is Java automatically converting a primitive into its wrapper type when an object is expected, like assigning an int directly to an Integer variable without an explicit cast.

Example: Understanding Autoboxing

java
public class Main {
	public static void main(String[] args) {
		Integer boxed = 5; // int automatically becomes Integer
		System.out.println(boxed);
	}
}

Understanding Unboxing

Unboxing is the reverse: Java automatically extracts the primitive value from a wrapper object when a primitive is expected, such as using an Integer in an arithmetic expression.

Example: Understanding Unboxing

java
public class Main {
	public static void main(String[] args) {
		Integer boxed = 10;
		int primitive = boxed + 5; // Integer automatically becomes int
		System.out.println(primitive);
	}
}

Parsing Numeric Strings

Methods like Integer.parseInt() and Double.parseDouble() convert a String into its corresponding primitive value, throwing NumberFormatException if the string isn't a valid number.

Example: Parsing Numeric Strings

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare num and set it to Integer.parseInt("42")
		int num = Integer.parseInt("42");
		// Declare d and set it to Double.parseDouble("3.14")
		double d = Double.parseDouble("3.14");
		// Print a line to the console
		System.out.println(num + " " + d);
	}
}

Wrapper Constants and Utility Methods

Wrapper classes also expose useful constants and utilities, like Integer.MAX_VALUE for the largest representable int, and Integer.compare() for a null-safe comparison alternative to </>.

Example: Wrapper Constants and Utility Methods

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Integer.MAX_VALUE);
		// Print a line to the console
		System.out.println(Integer.compare(3, 7));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Comparing two Integer objects with ==, which only works for small cached values; use equals().
  2. Unboxing a null wrapper, which throws NullPointerException.
  3. Parsing a non-numeric string with Integer.parseInt(), which throws NumberFormatException.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.