Java Wrapper Classes
Bridge primitive values and object APIs.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Wrapper Classes.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
Wrapper classes such as Integer represent primitive-like values as objects. Autoboxing and unboxing make many conversions implicit, which is convenient for collections. Null references and object identity can still surprise you.
Explore the details
Java Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean,
etc..) as objects.
The table below shows the primitive type and the equivalent wrapper class:
| Primitive Data Type | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |
Sometimes you must use wrapper classes, for example when working with Collection objects, such
as ArrayList, where primitive types cannot be
used (the list can only store objects):
Creating Wrapper Objects
To create a wrapper object, use the wrapper class instead of the primitive type. To get the value, you can just print the object:
Since you're now working with objects, you can use certain methods to get information about the specific object.
For example, the following methods are used to get the value associated with
the corresponding wrapper object: intValue(), byteValue(), shortValue(), longValue(),
floatValue(), doubleValue(), charValue(),
booleanValue().
This example will output the same result as the example above:
Another useful method is the toString() method, which is used to convert wrapper objects to strings.
// Auto-boxing: primitive → wrapper (automatic)
int num = 42;
Integer boxed = num; // autoboxing
int unboxed = boxed; // unboxing
// Useful static methods
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.parseInt("123")); // 123
System.out.println(Double.parseDouble("3.14")); // 3.14
System.out.println(Integer.toBinaryString(10)); // 1010
// Convert wrapper to String
String s = Integer.toString(255);
System.out.println(s.length()); // 3 (length of "255")In the following example, we convert an Integer to a String, and use the length() method of the String class to output the length of the "string":
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int value = 42;
Integer boxed = value;
int unboxed = boxed;
System.out.println(unboxed);
System.out.println(Integer.parseInt("125") + 5);
}
}Predict first. Then reveal the expected output / வெளியீடு
42 130
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Change the parseInt input to abc, then restore the valid number.
Reveal the answer and reasoning / விடை
Parsing abc throws NumberFormatException because it is not a valid decimal integer.
One mistake to avoid: Unboxing a null Integer throws NullPointerException. Use equals for wrapper value comparisons rather than relying on == reference identity.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.