Wrapper classes in Java convert primitive data types into objects. They are useful when working with collections, generics, and methods that require objects instead of primitive values.
Java has primitive types such as int, double, char, and boolean. Each primitive type has a corresponding wrapper class.
int number = 10;
Integer object = Integer.valueOf(number);
System.out.println(number);
System.out.println(object);
Output:
10
10
The primitive variable stores a primitive value, while the Integer variable refers to an object that represents an integer value.
| Primitive Type | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Notice that the wrapper class for int is
Integer, and the wrapper class for
char is Character.
Use methods such as Integer.valueOf() to
obtain wrapper objects from primitive values.
public class Main {
public static void main(String[] args) {
Integer number = Integer.valueOf(100);
Double price = Double.valueOf(99.50);
Character letter = Character.valueOf('A');
Boolean status = Boolean.valueOf(true);
System.out.println(number);
System.out.println(price);
System.out.println(letter);
System.out.println(status);
}
}
Output:
100
99.5
A
true
Wrapper classes are immutable. Once a wrapper object represents a value, that value cannot be changed inside the same object.
Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper type.
public class Main {
public static void main(String[] args) {
int number = 50;
Integer object = number;
System.out.println(number);
System.out.println(object);
}
}
Output:
50
50
Java automatically converts the int value into an Integer object in the assignment.
The following assignments also demonstrate autoboxing:
Integer a = 10;
Double b = 25.5;
Boolean c = true;
Character d = 'J';
Unboxing is the automatic conversion of a wrapper object into its corresponding primitive value.
public class Main {
public static void main(String[] args) {
Integer object = 100;
int number = object;
System.out.println(object);
System.out.println(number);
}
}
Output:
100
100
Java automatically converts the Integer object into an int value during assignment.
Wrapper classes provide parsing methods that convert numeric text into primitive values.
public class Main {
public static void main(String[] args) {
String a = "100";
String b = "25.5";
int number = Integer.parseInt(a);
double price = Double.parseDouble(b);
System.out.println(number + 50);
System.out.println(price + 10);
}
}
Output:
150
35.5
Common parsing methods include:
Byte.parseByte()Short.parseShort()Integer.parseInt()Long.parseLong()Float.parseFloat()Double.parseDouble()Boolean.parseBoolean()
Invalid numeric text can cause a
NumberFormatException.
Methods such as Integer.valueOf() can
convert numeric strings into wrapper objects.
public class Main {
public static void main(String[] args) {
Integer number = Integer.valueOf("250");
Double price = Double.valueOf("45.75");
System.out.println(number);
System.out.println(price);
}
}
Output:
250
45.75
Use parseInt() when you need an int primitive
and valueOf() when you need an Integer object.
Wrapper classes provide several helpful methods.
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Integer.compare(10, 20));
}
}
Important members include:
| Member | Purpose |
|---|---|
Integer.MAX_VALUE |
Largest int value. |
Integer.MIN_VALUE |
Smallest int value. |
Integer.parseInt() |
Parses text into an int. |
Integer.valueOf() |
Obtains an Integer from a value or numeric string. |
Double.parseDouble() |
Parses text into a double. |
Character.isLetter() |
Checks whether a character is a letter. |
Character.isDigit() |
Checks whether a character is a digit. |
Boolean.parseBoolean() |
Returns true for text equal to "true", ignoring case. |
Output from the first example:
2147483647
-2147483648
1.7976931348623157E308
-1
Java collections store objects rather than primitive
values. This is why we use Integer instead
of int in an ArrayList.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
int total = 0;
for (int number : numbers) {
total += number;
}
System.out.println("Total: " + total);
}
}
Output:
Total: 60
The primitive int values are automatically boxed when added and unboxed when retrieved in the loop.
The == operator compares object references
when used with wrapper objects. Use
equals() to compare their values.
public class Main {
public static void main(String[] args) {
Integer a = Integer.valueOf(1000);
Integer b = Integer.valueOf(1000);
System.out.println(a == b);
System.out.println(a.equals(b));
}
}
Output:
false
true
The two objects represent equal integer values, but
they are distinct objects. Do not rely on
== for wrapper value comparisons because
some wrapper values may be cached.
| Primitive Types | Wrapper Classes |
|---|---|
| Examples: int, double, char. | Examples: Integer, Double, Character. |
| Store primitive values. | Represent values as objects. |
| Cannot be null. | Object references can be null. |
| Can be used directly in arithmetic. | Can be automatically unboxed for arithmetic. |
| Can be used as array element types. | Can be used as generic type arguments. |
equals() rather than == to compare wrapper values.Example of safe parsing:
String input = "123";
try {
int number = Integer.parseInt(input);
System.out.println(number);
} catch (NumberFormatException e) {
System.out.println("Please enter a valid integer.");
}
1. What is the wrapper class for int?
2. What is autoboxing?
3. Which method converts a numeric string into an int?