In Java, wrapper classes are special classes that let you treat primitive data types as objects.
They “wrap” a primitive value in an object so it can be used where an object is required (for example, in collections like ArrayList
that can’t store primitives directly).
1. Primitive Types & Their Wrapper Classes
Primitive type | Wrapper class (java.lang) |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
2. Why We Need Wrapper Classes
- Collections:
ArrayList<Integer>
instead ofArrayList<int>
(Java generics don’t work with primitives). - Utility methods: Wrapper classes provide useful constants and methods (e.g.,
Integer.parseInt("123")
). - Type conversion: Converting between types and strings easily.
- Null values: Wrappers can be
null
; primitives cannot.
3. Autoboxing & Unboxing
Java automatically converts between primitive types and their wrapper classes.
Autoboxing: Primitive → Wrapper
int x = 5;
Integer obj = x; // Autoboxing
Unboxing: Wrapper → Primitive
Integer obj = 10;
int y = obj; // Unboxing
4. Common Methods in Wrapper Classes
Integer num = Integer.valueOf(42); // Create wrapper object
int val = num.intValue(); // Get primitive value
String s = Integer.toString(42); // Convert to string
int parsed = Integer.parseInt("123"); // String to int
5. Example Use Case
import java.util.ArrayList;
public class WrapperExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
// Autoboxing
list.add(10); // int -> Integer
list.add(20);
// Unboxing
int sum = list.get(0) + list.get(1); // Integer -> int
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 30
BigInteger
In Java, BigInteger
is a class in java.math
used for working with integers that are too large (or too small) to fit in primitive types like int
or long
.
For example:
int
can store values from about -2.1 billion to 2.1 billionlong
can store values from about -9 quintillion to 9 quintillionBigInteger
can store any size integer, limited only by available memory.
1. Import & Creation
You need to import it:
import java.math.BigInteger;
Creating BigInteger
objects:
BigInteger a = new BigInteger("123456789012345678901234567890");
BigInteger b = BigInteger.valueOf(12345); // from long
You must use strings for numbers too large for primitives.
*2. Operations (No +, -, , / Operators)
With BigInteger
, you can’t use normal arithmetic operators (+
, -
, *
, /
).
You must use methods:
Operation | Method Example |
---|---|
Addition | a.add(b) |
Subtraction | a.subtract(b) |
Multiplication | a.multiply(b) |
Division | a.divide(b) |
Modulus | a.mod(b) |
Power | a.pow(5) |
GCD | a.gcd(b) |
Comparison | a.compareTo(b) |
Example:
import java.math.BigInteger;
public class BigIntegerExample {
public static void main(String[] args) {
BigInteger a = new BigInteger("9999999999999999999999999999");
BigInteger b = new BigInteger("8888888888888888888888888888");
BigInteger sum = a.add(b);
BigInteger product = a.multiply(b);
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
}
}
3. Other Useful Methods
a.abs(); // absolute value
a.negate(); // negate
a.isProbablePrime(10); // primality check
a.shiftLeft(2); // multiply by 2^2
a.shiftRight(3); // divide by 2^3
4. When to Use
- Very large numbers (cryptography, factorials, combinatorics)
- When precision is critical (avoid overflow)
- Storing values outside
long
range