When working in Java, we often deal with primitive types like int, double, and boolean.
But what happens when we need to treat these as objects β for example, to store them in a collection like ArrayList, or pass them into object-based APIs?
Thatβs where Wrapper Classes come into play.
Java provides eight wrapper classes that correspond to its primitive types:
Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Wrapper classes βwrapβ primitive values into objects, allowing them to be used wherever an object is required.
π§ 1. Primitive to Wrapper (Boxing)
Boxing converts a primitive type into its corresponding wrapper object.
You can do this explicitly using the valueOf() method:
Integer intObj = Integer.valueOf(42); Double doubleObj = Double.valueOf(45.67); Boolean boolObj = Boolean.valueOf(true); Character charObj = Character.valueOf('A');
β Use case: When you need to store primitive values in collections or pass them as objects.
π 2. Wrapper to Primitive (Unboxing)
Unboxing converts a wrapper object back into a primitive value.
int intVal = intObj.intValue(); double doubleVal = doubleObj.doubleValue(); boolean boolVal = boolObj.booleanValue(); char charVal = charObj.charValue();
β Use case: When performing arithmetic or logical operations on values retrieved from wrapper objects.
π€ 3. String to Wrapper
If data arrives as text (e.g., from user input), you can convert strings directly into wrapper objects using valueOf().
Integer num = Integer.valueOf("123"); Double d = Double.valueOf("45.67"); Boolean b = Boolean.valueOf("true");
β Use case: Reading numbers from files or UI input as typed strings.
π¬ 4. Wrapper to String
To convert a wrapper object into a string, simply call toString().
String intStr = intObj.toString(); String doubleStr = doubleObj.toString(); String boolStr = boolObj.toString();
β Use case: Logging, displaying, or serializing data.
π’ 5. String to Primitive (Using parseXXX() Methods)
Each wrapper class provides a parseXXX() method to convert strings directly into primitive values.
int i = Integer.parseInt("200"); double d = Double.parseDouble("45.89"); boolean b = Boolean.parseBoolean("false");
β Use case: When you need primitive types without creating wrapper objects.
βοΈ 6. Autoboxing and Auto-unboxing
Java automatically converts between primitives and wrapper types β this is called autoboxing and auto-unboxing.
Integer autoBoxed = 50; // autoboxing (int β Integer) int autoUnboxed = autoBoxed; // auto-unboxing (Integer β int)
β Use case: Cleaner code when using collections or performing arithmetic with wrapper types.
βοΈ 7. equals() and compareTo()
Wrapper classes override equals() and compareTo() to compare values rather than references.
Integer num1 = 100, num2 = 100, num3 = 200; System.out.println(num1.equals(num2)); // true System.out.println(num1.compareTo(num3)); // -1 (less than) System.out.println(num3.compareTo(num1)); // 1 (greater than)
β Use case: Comparing numeric values safely in object form.
π 8. Wrapper Class Constants
Each numeric wrapper class defines useful constants like MIN_VALUE and MAX_VALUE.
System.out.println("Integer MIN: " + Integer.MIN_VALUE + ", MAX: " + Integer.MAX_VALUE); System.out.println("Double MIN: " + Double.MIN_VALUE + ", MAX: " + Double.MAX_VALUE); System.out.println("Float MIN: " + Float.MIN_VALUE + ", MAX: " + Float.MAX_VALUE);
β Use case: Validating input ranges and preventing overflow.
π» Full Example: Practicing All Conversions
Hereβs a single Java program that brings together all of these conversions in one place β great for classroom or self-study use.
public class WrapperConversionsDemo { public static void main(String[] args) { Integer intObj = Integer.valueOf(42); Double doubleObj = Double.valueOf(45.67); Boolean boolObj = Boolean.valueOf(true); System.out.println("-- Boxing and Unboxing --"); System.out.println("Integer: " + intObj.intValue()); System.out.println("Double: " + doubleObj.doubleValue()); System.out.println("Boolean: " + boolObj.booleanValue()); System.out.println("\n-- String to Wrapper and Primitive --"); Integer num = Integer.valueOf("123"); int parsed = Integer.parseInt("123"); System.out.println("Wrapper: " + num + ", Primitive: " + parsed); System.out.println("\n-- Wrapper to String --"); String str = num.toString(); System.out.println("String: " + str); System.out.println("\n-- Autoboxing and Auto-unboxing --"); Integer boxed = 10; int unboxed = boxed; System.out.println("Boxed: " + boxed + ", Unboxed: " + unboxed); } }
π§© Summary Table
Conversion Type Method / Concept Example
Primitive β Wrapper valueOf() Integer.valueOf(10)
Wrapper β Primitive intValue() int x = intObj.intValue()
String β Wrapper valueOf(String) Integer.valueOf(β20β)
Wrapper β String toString() intObj.toString()
String β Primitive parseXXX() Integer.parseInt(β50β)
Autoboxing Automatic Integer x = 10;
Auto-unboxing Automatic int y = x;
β¨ Final Thoughts
Wrapper classes form the bridge between Javaβs primitive world and its object-oriented design.
They enable you to take advantage of features like collections, generics, and APIs that operate on objects β without losing the speed and simplicity of primitive types.
By mastering these conversions, youβll gain a stronger command of data handling, parsing, and type management in Java β skills that are essential for writing clean, efficient, and modern code.
π§ Author: Phaneendra, Code Scholars Tutoring
π‘ Helping students and professionals master programming, algorithms, and cloud technologies with clarity and confidence.