Syntax
WrapperClass variable = primitiveValue; // autoboxingExamples
Basic Wrapper Classes
Each primitive type's corresponding wrapper class.
Integer boxedInt = 42;
Double boxedDouble = 3.14;
Boolean boxedBoolean = true;
Character boxedChar = 'A';
System.out.println(boxedInt);
System.out.println(boxedDouble);Autoboxing and Unboxing
Java automatically converts between primitives and their wrapper types where needed.
int primitive = 10;
Integer boxed = primitive; // autoboxing: int -> Integer
Integer wrapped = 25;
int unboxed = wrapped; // unboxing: Integer -> int
System.out.println(boxed + unboxed); // 35, works seamlesslyWhy Wrapper Classes Matter for Collections
Generic collections like ArrayList cannot hold primitives directly - they require wrapper classes.
import java.util.ArrayList;
// ArrayList<int> numbers = new ArrayList<>(); // Error! primitives not allowed
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10); // autoboxed to Integer automatically
numbers.add(20);
int first = numbers.get(0); // auto-unboxed back to int
System.out.println(first);Useful Wrapper Class Methods
Wrapper classes provide static utility methods for parsing and converting values.
String numText = "123";
int parsed = Integer.parseInt(numText);
System.out.println(parsed + 1); // 124
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.toBinaryString(10)); // 1010Best practices
- Use wrapper classes when working with generics and collections (ArrayList<Integer>, not ArrayList<int>), since generics require object types
- Prefer primitives over wrapper classes for simple local computation - they avoid the small overhead of object creation and unboxing
- Be careful comparing wrapper objects with == - like String, it compares references, not values, outside a small cached range (-128 to 127 for Integer)
- Watch for a NullPointerException when unboxing a wrapper object that is null - a null Integer cannot be auto-unboxed to int
At a glance
- Purpose
- General-purpose application development
- File extension
- .java
- Runs in
- Java Virtual Machine
- Usually used with
- JDK and Java libraries
Specifications & further reading
Related Java documentation
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.